1 # Copyright 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2 # 2003, 2004 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>.
28 if { [skip_cplus_tests] } { continue }
30 load_lib "cp-support.exp"
32 set testfile "classes"
33 set srcfile ${testfile}.cc
34 set binfile ${objdir}/${subdir}/${testfile}
36 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
37 gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
40 # Test ptype of class objects.
42 proc test_ptype_class_objects {} {
48 "ptype struct default_public_struct" "" "struct" "default_public_struct" \
50 { field public "int a;" }
51 { field public "int b;" }
54 # Another simple type.
57 "ptype struct explicit_public_struct" "" "struct" "explicit_public_struct" \
59 { field public "int a;" }
60 { field public "int b;" }
63 # Another simple type.
66 "ptype struct protected_struct" "" "struct" "protected_struct" \
68 { field protected "int a;" }
69 { field protected "int b;" }
72 # Another simple type.
75 "ptype struct private_struct" "" "struct" "private_struct" \
77 { field private "int a;" }
78 { field private "int b;" }
84 "ptype struct mixed_protection_struct" "" "struct" "mixed_protection_struct" \
86 { field public "int a;" }
87 { field public "int b;" }
88 { field private "int c;" }
89 { field private "int d;" }
90 { field protected "int e;" }
91 { field protected "int f;" }
92 { field public "int g;" }
93 { field private "int h;" }
94 { field protected "int i;" }
97 # All that again with "class" instead of "struct".
98 # gdb does not care about the difference anyways.
100 cp_test_ptype_class \
101 "ptype class public_class" "" "class" "public_class" \
103 { field public "int a;" }
104 { field public "int b;" }
107 # Another simple type.
109 cp_test_ptype_class \
110 "ptype class protected_class" "" "class" "protected_class" \
112 { field protected "int a;" }
113 { field protected "int b;" }
116 # Another simple type.
118 cp_test_ptype_class \
119 "ptype class default_private_class" "" "class" "default_private_class" \
121 { field private "int a;" }
122 { field private "int b;" }
125 # Another simple type.
127 cp_test_ptype_class \
128 "ptype class explicit_private_class" "" "class" "explicit_private_class" \
130 { field private "int a;" }
131 { field private "int b;" }
136 cp_test_ptype_class \
137 "ptype class mixed_protection_class" "" "class" "mixed_protection_class" \
140 { field public "int a;" }
141 { field public "int b;" }
142 { field private "int c;" }
143 { field private "int d;" }
144 { field protected "int e;" }
145 { field protected "int f;" }
146 { field public "int g;" }
147 { field private "int h;" }
148 { field protected "int i;" }
151 # Here are some classes with inheritance.
155 cp_test_ptype_class \
156 "ptype class A" "" "class" "A" \
158 { field public "int a;" }
159 { field public "int x;" }
164 cp_test_ptype_class \
165 "ptype class B" "" "class" "B" \
168 { field public "int b;" }
169 { field public "int x;" }
174 cp_test_ptype_class \
175 "ptype class C" "" "class" "C" \
178 { field public "int c;" }
179 { field public "int x;" }
182 # Derived class, multiple inheritance.
184 cp_test_ptype_class \
185 "ptype class D" "" "class" "D" \
189 { field public "int d;" }
190 { field public "int x;" }
195 cp_test_ptype_class \
196 "ptype class E" "" "class" "E" \
199 { field public "int e;" }
200 { field public "int x;" }
203 # This is a break from inheritance tests.
205 # gcc 2.X with stabs (stabs or stabs+?) used to have a problem with
206 # static methods whose name is the same as their argument mangling.
208 cp_test_ptype_class \
209 "ptype class Static" "" "class" "Static" \
211 { method public "static void ii(int, int);" }
214 # Here are some virtual inheritance tests.
216 # A virtual base class.
218 cp_test_ptype_class \
219 "ptype class vA" "" "class" "vA" \
221 { field public "int va;" }
222 { field public "int vx;" }
225 # A derived class with a virtual base.
227 cp_test_ptype_class \
228 "ptype class vB" "" "class" "vB" \
230 { base "public virtual vA" }
232 { field public "int vb;" }
233 { field public "int vx;" }
236 # Another derived class with a virtual base.
238 cp_test_ptype_class \
239 "ptype class vC" "" "class" "vC" \
241 { base "public virtual vA" }
243 { field public "int vc;" }
244 { field public "int vx;" }
247 # A classic diamond class.
249 cp_test_ptype_class \
250 "ptype class vD" "" "class" "vD" \
252 { base "public virtual vB" }
253 { base "public virtual vC" }
256 { field public "int vd;" }
257 { field public "int vx;" }
260 # A class derived from a diamond class.
262 cp_test_ptype_class \
263 "ptype class vE" "" "class" "vE" \
265 { base "public virtual vD" }
267 { field public "int ve;" }
268 { field public "int vx;" }
271 # Another inheritance series.
275 cp_test_ptype_class \
276 "ptype class Base1" "" "class" "Base1" \
278 { field public "int x;" }
279 { method public "Base1(int);" }
282 # Another base class.
284 cp_test_ptype_class \
285 "ptype class Foo" "" "class" "Foo" \
287 { field public "int x;" }
288 { field public "int y;" }
289 { field public "static int st;" }
290 { method public "Foo(int, int);" }
291 { method public "int operator!();" }
292 { method public "operator int();" }
293 { method public "int times(int);" }
299 "int operator int();"
300 { setup_kfail "gdb/1497" "*-*-*" }
304 "int operator int(void);"
305 { setup_kfail "gdb/1497" "*-*-*" }
309 # A multiple inheritance derived class.
311 cp_test_ptype_class \
312 "ptype class Bar" "" "class" "Bar" \
314 { base "public Base1" }
315 { base "public Foo" }
316 { field public "int z;" }
317 { method public "Bar(int, int, int);" }
322 # Test simple access to class members.
323 # TODO: these test names are gross!
324 # Just let the test name default.
326 proc test_non_inherited_member_access {} {
329 # Print non-inherited members of g_A.
330 gdb_test "print g_A.a" ".* = 1" "g_A.a incorrect"
331 gdb_test "print g_A.x" ".* = 2" "g_A.x incorrect"
333 # Print non-inherited members of g_B.
334 gdb_test "print g_B.b" ".* = 5" "g_B.b incorrect"
335 gdb_test "print g_B.x" ".* = 6" "g_B.x incorrect"
337 # Print non-inherited members of g_C.
338 gdb_test "print g_C.c" ".* = 9" "g_C.c incorrect"
339 gdb_test "print g_C.x" ".* = 10" "g_C.x incorrect"
341 # Print non-inherited members of g_D.
342 gdb_test "print g_D.d" ".* = 19" "g_D.d incorrect"
343 gdb_test "print g_D.x" ".* = 20" "g_D.x incorrect"
345 # Print non-inherited members of g_E.
346 gdb_test "print g_E.e" ".* = 31" "g_E.e incorrect"
347 gdb_test "print g_E.x" ".* = 32" "g_E.x incorrect"
350 # Test access to members of other classes.
351 # gdb should refuse to print them.
352 # (I feel old -- I remember when this was legal in C -- chastain).
353 # TODO: Again, change the silly test names.
355 proc test_wrong_class_members {} {
358 gdb_test "print g_A.b" "There is no member( or method|) named b." "print g_A.b should be error"
359 gdb_test "print g_B.c" "There is no member( or method|) named c." "print g_B.c should be error"
360 gdb_test "print g_B.d" "There is no member( or method|) named d." "print g_B.d should be error"
361 gdb_test "print g_C.b" "There is no member( or method|) named b." "print g_C.b should be error"
362 gdb_test "print g_C.d" "There is no member( or method|) named d." "print g_C.d should be error"
363 gdb_test "print g_D.e" "There is no member( or method|) named e." "print g_D.e should be error"
366 # Test access to names that are not members of any class.
367 # TODO: test names again.
369 proc test_nonexistent_members {} {
372 gdb_test "print g_A.y" "There is no member( or method|) named y." "print g_A.y should be error"
373 gdb_test "print g_B.z" "There is no member( or method|) named z." "print g_B.z should be error"
374 gdb_test "print g_C.q" "There is no member( or method|) named q." "print g_C.q should be error"
375 gdb_test "print g_D.p" "There is no member( or method|) named p." "print g_D.p should be error"
378 # Call a method that expects a base class parameter with base, inherited,
379 # and unrelated class arguments.
381 proc test_method_param_class {} {
382 gdb_test "call class_param.Aptr_a (&g_A)" ".* = 1" "base class param->a"
383 gdb_test "call class_param.Aptr_x (&g_A)" ".* = 2" "base class param->x"
384 gdb_test "call class_param.Aptr_a (&g_B)" ".* = 3" "inherited class param->a"
385 gdb_test "call class_param.Aptr_x (&g_B)" ".* = 4" "inherited class param->x"
386 gdb_test "call class_param.Aref_a (g_A)" ".* = 1" "base class (¶m)->a"
387 gdb_test "call class_param.Aref_x (g_A)" ".* = 2" "base class (¶m)->x"
388 gdb_test "call class_param.Aref_a (g_B)" ".* = 3" "inherited class (¶m)->a"
389 gdb_test "call class_param.Aref_x (g_B)" ".* = 4" "inherited class (¶m)->x"
390 gdb_test "call class_param.Aval_a (g_A)" ".* = 1" "base class param.a"
391 gdb_test "call class_param.Aval_x (g_A)" ".* = 2" "base class param.x"
392 gdb_test "call class_param.Aval_a (g_B)" ".* = 3" "inherited class param.a"
393 gdb_test "call class_param.Aval_x (g_B)" ".* = 4" "inherited class param.x"
395 gdb_test "call class_param.Aptr_a (&foo)" "Cannot resolve .*" "unrelated class *param"
396 gdb_test "call class_param.Aref_a (foo)" "Cannot resolve .*" "unrelated class ¶m"
397 gdb_test "call class_param.Aval_a (foo)" "Cannot resolve .*" "unrelated class param"
400 # Examine a class with an enum field.
409 gdb_test "print obj_with_enum" \
410 "\\$\[0-9\]+ = \{priv_enum = red, x = 0\}" \
411 "print obj_with_enum (1)"
417 # print the object again
419 gdb_test "print obj_with_enum" \
420 "\\$\[0-9\]+ = \{priv_enum = green, x = 0\}" \
421 "print obj_with_enum (2)"
423 # print the enum member
425 gdb_test "print obj_with_enum.priv_enum" "\\$\[0-9\]+ = green"
427 # ptype on the enum member
429 gdb_test_multiple "ptype obj_with_enum.priv_enum" "ptype obj_with_enum.priv_enum" {
430 -re "type = enum ClassWithEnum::PrivEnum \{ ?red, green, blue, yellow = 42 ?\}$nl$gdb_prompt $" {
431 pass "ptype obj_with_enum.priv_enum"
433 -re "type = enum PrivEnum \{ ?red, green, blue, yellow = 42 ?\}$nl$gdb_prompt $" {
434 # gcc 2.95.3 -gdwarf-2
435 # gcc 3.3.2 -gdwarf-2
436 pass "ptype obj_with_enum.priv_enum"
438 -re "type = enum \{ ?red, green, blue, yellow = 42 ?\}$nl$gdb_prompt $" {
439 # This case case is a little dubious, but it's not clear what
440 # ought to be required of a ptype on a private enum...
443 # It bugs me that this happens with gcc 3.
444 # -- chastain 2003-12-30
446 # gcc 2.95.3 -gstabs+
448 # gcc HEAD 2003-12-28 21:08:30 UTC -gstabs+
449 pass "ptype obj_with_enum.priv_enum"
453 # ptype on the object
455 # NOTE: carlton/2003-02-28: One could certainly argue that plain
457 # is acceptable: PrivEnum is a member of ClassWithEnum, so
458 # there's no need to explicitly qualify its name with
459 # "ClassWithEnum::". The truth, though, is that GDB is simply
460 # forgetting that PrivEnum is a member of ClassWithEnum, so we do
461 # that output for a bad reason instead of a good reason. Under
462 # stabs, we probably can't get this right; under DWARF-2, we can.
464 cp_test_ptype_class \
465 "ptype obj_with_enum" "" "class" "ClassWithEnum" \
467 { field public "ClassWithEnum::PrivEnum priv_enum;" }
468 { field public "int x;" }
473 "ClassWithEnum::PrivEnum priv_enum;"
474 "PrivEnum priv_enum;"
475 { setup_kfail "gdb/57" "*-*-*" }
479 # I'll do this test two different ways, because of a parser bug.
482 gdb_test_multiple "print (ClassWithEnum::PrivEnum) 42" "print (ClassWithEnum::PrivEnum) 42" {
483 -re "\\$\[0-9\]+ = yellow$nl$gdb_prompt $" {
484 pass "print (ClassWithEnum::PrivEnum) 42"
486 -re "A (parse|syntax) error in expression, near `42'.$nl$gdb_prompt $" {
487 # "parse error" is bison 1.35.
488 # "syntax error" is bison 1.875.
489 kfail "gdb/1588" "print (ClassWithEnum::PrivEnum) 42"
493 gdb_test_multiple "print ('ClassWithEnum::PrivEnum') 42" "print ('ClassWithEnum::PrivEnum') 42" {
494 -re "\\$\[0-9\]+ = yellow$nl$gdb_prompt $" {
496 # gcc HEAD 2003-12-28 21:08:30 UTC -gstabs+
497 pass "print ('ClassWithEnum::PrivEnum') 42"
499 -re "No symbol \"ClassWithEnum::PrivEnum\" in current context.$nl$gdb_prompt $" {
500 # gcc 2.95.3 -gdwarf-2
501 # gcc 3.3.2 -gdwarf-2
502 # gcc HEAD 2003-12-28 21:08:30 UTC -gdwarf-2
503 # gcc 2.95.3 -gstabs+
504 kfail "gdb/57" "print ('ClassWithEnum::PrivEnum') 42"
509 # Pointers to class members
511 proc test_pointers_to_class_members {} {
514 gdb_test "print Bar::z" "\\$\[0-9\]+ = \\(int ?\\( ?Bar::& ?\\) ?\\) ?Bar::z"
515 gdb_test "print &Foo::x" "\\$\[0-9\]+ = \\(int ?\\( ?Foo::\\* ?\\) ?\\) ?&Foo::x"
516 gdb_test "print (int)&Foo::x" "\\$\[0-9\]+ = 0"
517 gdb_test "print (int)&Bar::y == 2*sizeof(int)" "\\$\[0-9\]+ = true"
519 # TODO: this is a bogus test. It's looking at a variable that
520 # has not even been declared yet, so it's accessing random junk
521 # on the stack and comparing that it's NOT equal to a specific
522 # value. It's been like this since gdb 4.10 in 1993!
523 # -- chastain 2004-01-01
524 gdb_test "print (int)pmi == sizeof(int)" ".* = false"
527 # Test static members.
529 proc test_static_members {} {
533 gdb_test "print Foo::st" "\\$\[0-9\]+ = 100"
534 gdb_test "set foo.st = 200" "" ""
535 gdb_test "print bar.st" "\\$\[0-9\]+ = 200"
536 gdb_test "print &foo.st" "\\$\[0-9\]+ = \\(int ?\\*\\) $hex"
537 gdb_test "print &Bar::st" "\\$\[0-9\]+ = \\(int ?\\*\\) $hex"
538 gdb_test "print *\$" "\\$\[0-9\]+ = 200"
540 gdb_test "set print static-members off" ""
541 gdb_test "print csi" \
543 "print csi without static members"
544 gdb_test "print cnsi" \
546 "print cnsi without static members"
548 gdb_test "set print static-members on" ""
549 gdb_test "print csi" \
550 "{x = 10, y = 20, static null = {x = 0, y = 0, static null = <same as static member of an already seen type>}}" \
551 "print csi with static members"
552 gdb_test "print cnsi" \
553 "{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>}" \
554 "print cnsi with static members"
570 # Start with a fresh gdb.
574 gdb_reinitialize_dir $srcdir/$subdir
577 gdb_test "set language c++" "" ""
578 gdb_test "set width 0" "" ""
580 if ![runto_main ] then {
581 perror "couldn't run to breakpoint"
585 gdb_breakpoint inheritance2
586 gdb_test "continue" ".*Breakpoint .* inheritance2.*" ""
588 test_ptype_class_objects
589 test_non_inherited_member_access
590 test_wrong_class_members
591 test_nonexistent_members
592 test_method_param_class
594 gdb_breakpoint enums2
595 gdb_test "continue" ".*Breakpoint .* enums2.*" "continue to enums2(\\(\\)|)"
596 gdb_test "finish" "" ""
599 gdb_test "finish" "" ""
600 test_pointers_to_class_members
603 # Now some random tests that were just thrown in here.
605 gdb_breakpoint marker_reg1
606 gdb_test "continue" ".*Breakpoint .* marker_reg1.*" ""
607 gdb_test "finish" "Run till exit from.*" "finish from marker_reg1"
609 # This class is so small that an instance of it can fit in a register.
610 # When gdb tries to call a method, it gets embarrassed about taking
611 # the address of a register.
613 # TODO: I think that message should be a PASS, not an XFAIL.
614 # gdb prints an informative message and declines to do something
617 # The method call actually succeeds if the compiler allocates very
618 # small classes in memory instead of registers. So this test does
619 # not tell us anything interesting if the call succeeds.
621 # -- chastain 2003-12-31
622 gdb_test_multiple "print v.method ()" "calling method for small class" {
623 -re "\\$\[0-9\]+ = 82$nl$gdb_prompt $" {
624 # gcc 3.3.2 -gdwarf-2
625 # gcc HEAD 2003-12-28 21:08:30 UTC -gdwarf-2
627 # gcc HEAD 2003-12-28 21:08:30 UTC -gstabs+
628 pass "calling method for small class"
630 -re "Address requested for identifier \"v\" which is in register .*$nl$gdb_prompt $" {
631 # gcc 2.95.3 -gdwarf-2
632 # gcc 2.95.3 -gstabs+
633 setup_xfail "*-*-*" 2972
634 fail "calling method for small class"
638 # This is a random v2 demangling test.
639 # This is redundant with existing tests in demangle.exp.
640 # TODO: Just remove this.
641 gdb_test "maint demangle inheritance1__Fv" "inheritance1\\(void\\)" "demangle"