1 # Copyright
1992-2015 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"
27 if {[prepare_for_testing $testfile.exp $testfile $srcfile
{debug c
++}]} {
31 # Test ptype of class objects.
33 proc test_ptype_class_objects
{} {
38 "struct default_public_struct" "" "struct" "default_public_struct" \
40 { field public
"int a;" }
41 { field public
"int b;" }
44 # Another simple type.
47 "struct explicit_public_struct" "" "struct" "explicit_public_struct" \
49 { field public
"int a;" }
50 { field public
"int b;" }
53 # Another simple type.
56 "struct protected_struct" "" "struct" "protected_struct" \
58 { field protected
"int a;" }
59 { field protected
"int b;" }
62 # Another simple type.
65 "struct private_struct" "" "struct" "private_struct" \
67 { field private
"int a;" }
68 { field private
"int b;" }
74 "struct mixed_protection_struct" "" "struct" "mixed_protection_struct" \
76 { field public
"int a;" }
77 { field public
"int b;" }
78 { field private
"int c;" }
79 { field private
"int d;" }
80 { field protected
"int e;" }
81 { field protected
"int f;" }
82 { field public
"int g;" }
83 { field private
"int h;" }
84 { field protected
"int i;" }
87 # All that again with
"class" instead of "struct".
88 # gdb does not care about the difference anyways.
91 "class public_class" "" "class" "public_class" \
93 { field public
"int a;" }
94 { field public
"int b;" }
97 # Another simple type.
100 "class protected_class" "" "class" "protected_class" \
102 { field protected
"int a;" }
103 { field protected
"int b;" }
106 # Another simple type.
108 cp_test_ptype_class \
109 "class default_private_class" "" "class" "default_private_class" \
111 { field private
"int a;" }
112 { field private
"int b;" }
115 # Another simple type.
117 cp_test_ptype_class \
118 "class explicit_private_class" "" "class" "explicit_private_class" \
120 { field private
"int a;" }
121 { field private
"int b;" }
126 cp_test_ptype_class \
127 "class mixed_protection_class" "" "class" "mixed_protection_class" \
130 { field public
"int a;" }
131 { field public
"int b;" }
132 { field private
"int c;" }
133 { field private
"int d;" }
134 { field protected
"int e;" }
135 { field protected
"int f;" }
136 { field public
"int g;" }
137 { field private
"int h;" }
138 { field protected
"int i;" }
141 # Here are some classes with inheritance.
145 cp_test_ptype_class \
146 "class A" "" "class" "A" \
148 { field public
"int a;" }
149 { field public
"int x;" }
154 cp_test_ptype_class \
155 "class B" "" "class" "B" \
158 { field public
"int b;" }
159 { field public
"int x;" }
164 cp_test_ptype_class \
165 "class C" "" "class" "C" \
168 { field public
"int c;" }
169 { field public
"int x;" }
172 # Derived class
, multiple inheritance.
174 cp_test_ptype_class \
175 "class D" "" "class" "D" \
179 { field public
"int d;" }
180 { field public
"int x;" }
185 cp_test_ptype_class \
186 "class E" "" "class" "E" \
189 { field public
"int e;" }
190 { field public
"int x;" }
193 # This is a
break from inheritance tests.
195 # gcc
2.X with stabs
(stabs or stabs
+?
) used to have a problem with
196 # static methods whose
name is the same as their
argument mangling.
198 cp_test_ptype_class \
199 "class Static" "" "class" "Static" \
201 { method public
"static void ii(int, int);" }
204 # Here are some virtual inheritance tests.
206 # A virtual base class.
208 cp_test_ptype_class \
209 "class vA" "" "class" "vA" \
211 { field public
"int va;" }
212 { field public
"int vx;" }
215 # A derived class with a virtual base.
217 cp_test_ptype_class \
218 "class vB" "" "class" "vB" \
220 { base
"public virtual vA" }
222 { field public
"int vb;" }
223 { field public
"int vx;" }
226 # Another derived class with a virtual base.
228 cp_test_ptype_class \
229 "class vC" "" "class" "vC" \
231 { base
"public virtual vA" }
233 { field public
"int vc;" }
234 { field public
"int vx;" }
237 # A classic diamond class.
239 cp_test_ptype_class \
240 "class vD" "" "class" "vD" \
242 { base
"public virtual vB" }
243 { base
"public virtual vC" }
246 { field public
"int vd;" }
247 { field public
"int vx;" }
250 # A class derived from a diamond class.
252 cp_test_ptype_class \
253 "class vE" "" "class" "vE" \
255 { base
"public virtual vD" }
257 { field public
"int ve;" }
258 { field public
"int vx;" }
261 # Another inheritance series.
265 cp_test_ptype_class \
266 "class Base1" "" "class" "Base1" \
268 { field public
"int x;" }
269 { method public
"Base1(int);" }
272 # Another base class.
274 cp_test_ptype_class \
275 "class Foo" "" "class" "Foo" \
277 { field public
"int x;" }
278 { field public
"int y;" }
279 { field public
"static int st;" }
280 { method public
"Foo(int, int);" }
281 { method public
"int operator!();" }
282 { method public
"operator int();" }
283 { method public
"int times(int);" }
289 "int operator int();"
290 { setup_kfail
"gdb/1497" "*-*-*" }
294 "int operator int(void);"
295 { setup_kfail
"gdb/1497" "*-*-*" }
299 # A multiple inheritance derived class.
301 cp_test_ptype_class \
302 "class Bar" "" "class" "Bar" \
304 { base
"public Base1" }
305 { base
"public Foo" }
306 { field public
"int z;" }
307 { method public
"Bar(int, int, int);" }
310 # Derived class with typedef
'd baseclass with virtual methods.
312 cp_test_ptype_class \
313 "class DynamicBar" "" "class" "DynamicBar" \
315 { base "public DynamicBase2" }
316 { field public "int y;" }
317 { method public "DynamicBar(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 # We match the enum values with and without qualifiers. As of
399 # 2008-08-21 we can output the qualifiers for DWARF-2.
401 gdb_test "print obj_with_enum" \
402 "\\$\[0-9\]+ = \{priv_enum = (ClassWithEnum::)?red, x = 0\}" \
403 "print obj_with_enum (1)"
409 # print the object again
411 gdb_test "print obj_with_enum" \
412 "\\$\[0-9\]+ = \{priv_enum = (ClassWithEnum::)?green, x = 0\}" \
413 "print obj_with_enum (2)"
415 # print the enum member
417 gdb_test "print obj_with_enum.priv_enum" "\\$\[0-9\]+ = (ClassWithEnum::)?green"
419 # ptype on the enum member
421 gdb_test_multiple "ptype obj_with_enum.priv_enum" "ptype obj_with_enum.priv_enum" {
422 -re "type = enum ClassWithEnum::PrivEnum (: unsigned int )?\{ ?(ClassWithEnum::)?red, (ClassWithEnum::)?green, (ClassWithEnum::)?blue, (ClassWithEnum::)?yellow = 42 ?\}$nl$gdb_prompt $" {
423 pass "ptype obj_with_enum.priv_enum"
425 -re "type = enum PrivEnum \{ ?(ClassWithEnum::)?red, (ClassWithEnum::)?green, (ClassWithEnum::)?blue, (ClassWithEnum::)?yellow = 42 ?\}$nl$gdb_prompt $" {
426 # gcc 2.95.3 -gdwarf-2
427 # gcc 3.3.2 -gdwarf-2
428 pass "ptype obj_with_enum.priv_enum"
430 -re "type = enum \{ ?red, green, blue, yellow = 42 ?\}$nl$gdb_prompt $" {
431 # This case case is a little dubious, but it's not clear what
432 # ought to be required of a ptype
on a private enum...
435 # It bugs me that this happens with gcc
3.
436 #
-- chastain
2003-12-30
438 # gcc
2.95.3 -gstabs
+
440 # gcc HEAD
2003-12-28 21:08:30 UTC
-gstabs
+
441 pass
"ptype obj_with_enum.priv_enum"
445 # ptype
on the object
447 # NOTE
: carlton
/2003-02-28: One could certainly argue that plain
449 # is acceptable
: PrivEnum is a member of ClassWithEnum
, so
450 # there
's no need to explicitly qualify its name with
451 # "ClassWithEnum::". The truth, though, is that GDB is simply
452 # forgetting that PrivEnum is a member of ClassWithEnum, so we do
453 # that output for a bad reason instead of a good reason. Under
454 # stabs, we probably can't
get this right
; under DWARF
-2, we can.
456 cp_test_ptype_class \
457 "obj_with_enum" "" "class" "ClassWithEnum" \
459 { field public
"ClassWithEnum::PrivEnum priv_enum;" }
460 { field public
"int x;" }
465 "ClassWithEnum::PrivEnum priv_enum;"
466 "PrivEnum priv_enum;"
467 { setup_kfail
"gdb/57" "*-*-*" }
471 # I
'll do this test two different ways, because of a parser bug.
474 gdb_test_multiple "print (ClassWithEnum::PrivEnum) 42" "print (ClassWithEnum::PrivEnum) 42" {
475 -re "\\$\[0-9\]+ = (ClassWithEnum::)?yellow$nl$gdb_prompt $" {
476 pass "print (ClassWithEnum::PrivEnum) 42"
478 -re "A (parse|syntax) error in expression, near `42'.$nl$gdb_prompt $
" {
479 #
"parse error" is bison 1.35.
480 #
"syntax error" is bison 1.875.
481 kfail
"gdb/1588" "print (ClassWithEnum::PrivEnum) 42"
485 gdb_test_multiple
"print ('ClassWithEnum::PrivEnum') 42" "print ('ClassWithEnum::PrivEnum') 42" {
486 -re
"\\$\[0-9\]+ = (ClassWithEnum::)?yellow$nl$gdb_prompt $" {
488 # gcc HEAD
2003-12-28 21:08:30 UTC
-gstabs
+
489 pass
"print ('ClassWithEnum::PrivEnum') 42"
491 -re
"No symbol \"ClassWithEnum::PrivEnum\" in current context.$nl$gdb_prompt $" {
492 # gcc
2.95.3 -gdwarf
-2
493 # gcc
3.3.2 -gdwarf
-2
494 # gcc HEAD
2003-12-28 21:08:30 UTC
-gdwarf
-2
495 # gcc
2.95.3 -gstabs
+
496 kfail
"gdb/57" "print ('ClassWithEnum::PrivEnum') 42"
501 # Pointers to class members
503 proc test_pointers_to_class_members
{} {
504 gdb_test
"print Bar::z" "Cannot reference non-static field \"z\""
505 gdb_test
"print &Foo::x" "\\$\[0-9\]+ = &Foo::x"
506 gdb_test
"print (int)&Foo::x" "\\$\[0-9\]+ = 0"
507 gdb_test
"print (int)&Bar::y == 2*sizeof(int)" "\\$\[0-9\]+ = true"
509 gdb_test
"ptype Bar::z" "type = int"
510 gdb_test
"ptype &Bar::z" "type = int Bar::\\*"
512 # TODO
: this is a bogus test. It
's looking at a variable that
513 # has not even been declared yet, so it's accessing
random junk
514 #
on the stack and comparing that it
's NOT equal to a specific
515 # value. It's been like this since gdb
4.10 in
1993!
516 #
-- chastain
2004-01-01
517 gdb_test
"print (int)pmi == sizeof(int)" ".* = false"
520 # Test static members.
522 proc test_static_members
{} {
525 gdb_test
"print Foo::st" "\\$\[0-9\]+ = 100"
526 gdb_test_no_output
"set foo.st = 200" ""
527 gdb_test
"print bar.st" "\\$\[0-9\]+ = 200"
528 gdb_test
"print &foo.st" "\\$\[0-9\]+ = \\(int ?\\*\\) $hex <Foo::st>"
529 gdb_test
"print &Bar::st" "\\$\[0-9\]+ = \\(int ?\\*\\) $hex <Foo::st>"
530 gdb_test
"print *\$" "\\$\[0-9\]+ = 200"
532 gdb_test_no_output
"set print static-members off"
533 gdb_test
"print csi" \
535 "print csi without static members"
536 gdb_test
"print cnsi" \
538 "print cnsi without static members"
540 gdb_test_no_output
"set print static-members on"
541 gdb_test
"print csi" \
542 "{x = 10, y = 20, static null = {x = 0, y = 0, static null = <same as static member of an already seen type>}}" \
543 "print csi with static members"
544 gdb_test
"print cnsi" \
545 "{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>}" \
546 "print cnsi with static members"
554 gdb_test_no_output
"set language c++" ""
555 gdb_test_no_output
"set width 0" ""
557 if ![runto_main
] then {
558 perror
"couldn't run to breakpoint"
562 gdb_breakpoint inheritance2
563 gdb_test
"continue" ".*Breakpoint .* inheritance2.*" ""
565 test_ptype_class_objects
566 test_non_inherited_member_access
567 test_wrong_class_members
568 test_nonexistent_members
569 test_method_param_class
571 gdb_breakpoint enums2
572 gdb_test
"continue" ".*Breakpoint .* enums2.*" "continue to enums2(\\(\\)|)"
573 # Leave enums2. Make sure we reach the next line
, in case there
574 # are
any more instructions to finish the function
call.
575 gdb_test_multiple
"finish" "" {
576 -re
"enums2 \\(\\);.*$gdb_prompt $" {
577 gdb_test
"next" ".*" ""
579 -re
"$gdb_prompt $" { }
583 gdb_test
"finish" ".*" ""
584 test_pointers_to_class_members
587 # Now some
random tests that were just thrown in here.
589 gdb_breakpoint marker_reg1
590 gdb_test
"continue" ".*Breakpoint .* marker_reg1.*" ""
591 gdb_test
"finish" "Run till exit from.*" "finish from marker_reg1"
593 # This class is so small that an instance of it can fit in a register.
594 # When gdb tries to
call a method
, it gets embarrassed about taking
595 # the address of a register.
597 # TODO
: I think that message should be a PASS
, not an XFAIL.
598 # gdb prints an informative message and declines to
do something
601 # The method
call actually succeeds
if the compiler allocates very
602 # small classes in memory instead of registers. So this test does
603 # not tell us anything interesting
if the
call succeeds.
605 #
-- chastain
2003-12-31
606 gdb_test_multiple
"print v.method ()" "calling method for small class" {
607 -re
"\\$\[0-9\]+ = 82$nl$gdb_prompt $" {
608 # gcc
3.3.2 -gdwarf
-2
609 # gcc HEAD
2003-12-28 21:08:30 UTC
-gdwarf
-2
611 # gcc HEAD
2003-12-28 21:08:30 UTC
-gstabs
+
612 pass
"calling method for small class"
614 -re
"Address requested for identifier \"v\" which is in register .*$nl$gdb_prompt $" {
615 # gcc
2.95.3 -gdwarf
-2
616 # gcc
2.95.3 -gstabs
+
617 setup_xfail
"*-*-*" 2972
618 fail
"calling method for small class"
622 gdb_test
"print base1::Base1" "<.*Base1.*>" "print ctor of typedef class"
623 gdb_test
"print base1::~Base1" "<.*~Base1(\\(\\))?>" \
624 "print dtor of typedef class"
626 gdb_test
"list ByAnyOtherName::times" ".*int Foo::times.*"