Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.cp / classes.exp
1 # Copyright 1992-2022 Free Software Foundation, Inc.
2
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.
7 #
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.
12 #
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/>.
15
16 # This file was written by Fred Fish. (fnf@cygnus.com)
17 # And rewritten by Michael Chastain <mec.gnu@mindspring.com>.
18
19 set nl "\[\r\n\]+"
20
21 if { [skip_cplus_tests] } { continue }
22
23 load_lib "cp-support.exp"
24
25 standard_testfile .cc
26
27 if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
28 return -1
29 }
30
31 # Test ptype of class objects.
32
33 proc test_ptype_class_objects {} {
34
35 # Simple type.
36
37 cp_test_ptype_class \
38 "struct default_public_struct" "" "struct" "default_public_struct" \
39 {
40 { field public "int a;" }
41 { field public "int b;" }
42 }
43
44 # Another simple type.
45
46 cp_test_ptype_class \
47 "struct explicit_public_struct" "" "struct" "explicit_public_struct" \
48 {
49 { field public "int a;" }
50 { field public "int b;" }
51 }
52
53 # Another simple type.
54
55 cp_test_ptype_class \
56 "struct protected_struct" "" "struct" "protected_struct" \
57 {
58 { field protected "int a;" }
59 { field protected "int b;" }
60 }
61
62 # Another simple type.
63
64 cp_test_ptype_class \
65 "struct private_struct" "" "struct" "private_struct" \
66 {
67 { field private "int a;" }
68 { field private "int b;" }
69 }
70
71 # A bigger type.
72
73 cp_test_ptype_class \
74 "struct mixed_protection_struct" "" "struct" "mixed_protection_struct" \
75 {
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;" }
85 }
86
87 # All that again with "class" instead of "struct".
88 # gdb does not care about the difference anyways.
89
90 cp_test_ptype_class \
91 "class public_class" "" "class" "public_class" \
92 {
93 { field public "int a;" }
94 { field public "int b;" }
95 }
96
97 # Another simple type.
98
99 cp_test_ptype_class \
100 "class protected_class" "" "class" "protected_class" \
101 {
102 { field protected "int a;" }
103 { field protected "int b;" }
104 }
105
106 # Another simple type.
107
108 cp_test_ptype_class \
109 "class default_private_class" "" "class" "default_private_class" \
110 {
111 { field private "int a;" }
112 { field private "int b;" }
113 }
114
115 # Another simple type.
116
117 cp_test_ptype_class \
118 "class explicit_private_class" "" "class" "explicit_private_class" \
119 {
120 { field private "int a;" }
121 { field private "int b;" }
122 }
123
124 # A bigger type.
125
126 cp_test_ptype_class \
127 "class mixed_protection_class" "" "class" "mixed_protection_class" \
128 {
129
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;" }
139 }
140
141 # Here are some classes with inheritance.
142
143 # Base class.
144
145 cp_test_ptype_class \
146 "class A" "" "class" "A" \
147 {
148 { field public "int a;" }
149 { field public "int x;" }
150 }
151
152 # Derived class.
153
154 cp_test_ptype_class \
155 "class B" "" "class" "B" \
156 {
157 { base "public A" }
158 { field public "int b;" }
159 { field public "int x;" }
160 }
161
162 # Derived class.
163
164 cp_test_ptype_class \
165 "class C" "" "class" "C" \
166 {
167 { base "public A" }
168 { field public "int c;" }
169 { field public "int x;" }
170 }
171
172 # Derived class, multiple inheritance.
173
174 cp_test_ptype_class \
175 "class D" "" "class" "D" \
176 {
177 { base "public B" }
178 { base "public C" }
179 { field public "int d;" }
180 { field public "int x;" }
181 }
182
183 # Derived class.
184
185 cp_test_ptype_class \
186 "class E" "" "class" "E" \
187 {
188 { base "public D" }
189 { field public "int e;" }
190 { field public "int x;" }
191 }
192
193 # This is a break from inheritance tests.
194 #
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.
197
198 cp_test_ptype_class \
199 "class Static" "" "class" "Static" \
200 {
201 { method public "static void ii(int, int);" }
202 }
203
204 # Here are some virtual inheritance tests.
205
206 # A virtual base class.
207
208 cp_test_ptype_class \
209 "class vA" "" "class" "vA" \
210 {
211 { field public "int va;" }
212 { field public "int vx;" }
213 }
214
215 # A derived class with a virtual base.
216
217 cp_test_ptype_class \
218 "class vB" "" "class" "vB" \
219 {
220 { base "public virtual vA" }
221 { vbase "vA" }
222 { field public "int vb;" }
223 { field public "int vx;" }
224 }
225
226 # Another derived class with a virtual base.
227
228 cp_test_ptype_class \
229 "class vC" "" "class" "vC" \
230 {
231 { base "public virtual vA" }
232 { vbase "vA" }
233 { field public "int vc;" }
234 { field public "int vx;" }
235 }
236
237 # A classic diamond class.
238
239 cp_test_ptype_class \
240 "class vD" "" "class" "vD" \
241 {
242 { base "public virtual vB" }
243 { base "public virtual vC" }
244 { vbase "vC" }
245 { vbase "vB" }
246 { field public "int vd;" }
247 { field public "int vx;" }
248 }
249
250 # A class derived from a diamond class.
251
252 cp_test_ptype_class \
253 "class vE" "" "class" "vE" \
254 {
255 { base "public virtual vD" }
256 { vbase "vD" }
257 { field public "int ve;" }
258 { field public "int vx;" }
259 }
260
261 # Another inheritance series.
262
263 # A base class.
264
265 cp_test_ptype_class \
266 "class Base1" "" "class" "Base1" \
267 {
268 { field public "int x;" }
269 { method public "Base1(int);" }
270 }
271
272 # Another base class.
273
274 cp_test_ptype_class \
275 "class Foo" "" "class" "Foo" \
276 {
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);" }
284 } \
285 "" \
286 {
287 {
288 "operator int();"
289 "int operator int();"
290 { setup_kfail "gdb/1497" "*-*-*" }
291 }
292 {
293 "operator int();"
294 "int operator int(void);"
295 { setup_kfail "gdb/1497" "*-*-*" }
296 }
297 }
298
299 # A multiple inheritance derived class.
300
301 cp_test_ptype_class \
302 "class Bar" "" "class" "Bar" \
303 {
304 { base "public Base1" }
305 { base "public Foo" }
306 { field public "int z;" }
307 { method public "Bar(int, int, int);" }
308 }
309
310 # Derived class with typedef'd baseclass with virtual methods.
311
312 cp_test_ptype_class \
313 "class DynamicBar" "" "class" "DynamicBar" \
314 {
315 { base "public DynamicBase2" }
316 { field public "int y;" }
317 { method public "DynamicBar(int, int);" }
318 }
319
320 # Classes with typedefs of different access.
321
322 cp_test_ptype_class \
323 "class class_with_typedefs" "" "class" "class_with_typedefs" \
324 {
325 { field protected \
326 "class_with_typedefs::public_int public_int_;" }
327 { field protected \
328 "class_with_typedefs::protected_int protected_int_;" }
329 { field protected \
330 "class_with_typedefs::private_int private_int_;" }
331 { method public "class_with_typedefs(void);" }
332 { method public "class_with_typedefs::public_int add_public(class_with_typedefs::public_int);" }
333 { method public \
334 "class_with_typedefs::public_int add_all(int);" }
335 { method protected "class_with_typedefs::protected_int add_protected(class_with_typedefs::protected_int);" }
336 { method private "class_with_typedefs::private_int add_private(class_with_typedefs::private_int);" }
337 { typedef public "typedef int public_int;" }
338 { typedef protected "typedef int protected_int;" }
339 { typedef private "typedef int private_int;" }
340 }
341
342 cp_test_ptype_class \
343 "class class_with_public_typedef" "" "class" \
344 "class_with_public_typedef" {
345 { field private "int a;" }
346 { field public "class_with_public_typedef::INT b;" }
347 { typedef public "typedef int INT;" }
348 }
349 cp_test_ptype_class \
350 "class class_with_protected_typedef" "" "class" \
351 "class_with_protected_typedef" {
352 { field private "int a;" }
353 { field protected "class_with_protected_typedef::INT b;" }
354 { typedef protected "typedef int INT;" }
355 }
356 cp_test_ptype_class \
357 "struct struct_with_protected_typedef" "" "struct" \
358 "struct_with_protected_typedef" {
359 { field public "int a;" }
360 { field protected "struct_with_protected_typedef::INT b;" }
361 { typedef protected "typedef int INT;" }
362 }
363 cp_test_ptype_class \
364 "struct struct_with_private_typedef" "" "struct" \
365 "struct_with_private_typedef" {
366 { field public "int a;" }
367 { field private "struct_with_private_typedef::INT b;" }
368 { typedef private "typedef int INT;" }
369 }
370
371 # For the following two cases, we cannot use cp_test_ptype_class.
372 # We need to explicitly check whether the access label was suppressed.
373 set ws {[ \t\r\n]*}
374 foreach {tag lbl} {"class" "private" "struct" "public"} {
375 gdb_test "ptype/r ${tag}_with_${lbl}_typedef" "type = $tag ${tag}_with_${lbl}_typedef \{${ws}int a;${ws}${tag}_with_${lbl}_typedef::INT b;${ws}typedef int INT;${ws}\}"
376 }
377 }
378
379 # Test simple access to class members.
380
381 proc test_non_inherited_member_access {} {
382
383 # Print non-inherited members of g_A.
384 gdb_test "print g_A.a" ".* = 1"
385 gdb_test "print g_A.x" ".* = 2"
386
387 # Print non-inherited members of g_B.
388 gdb_test "print g_B.b" ".* = 5"
389 gdb_test "print g_B.x" ".* = 6"
390
391 # Print non-inherited members of g_C.
392 gdb_test "print g_C.c" ".* = 9"
393 gdb_test "print g_C.x" ".* = 10"
394
395 # Print non-inherited members of g_D.
396 gdb_test "print g_D.d" ".* = 19"
397 gdb_test "print g_D.x" ".* = 20"
398
399 # Print non-inherited members of g_E.
400 gdb_test "print g_E.e" ".* = 31"
401 gdb_test "print g_E.x" ".* = 32"
402 }
403
404 # Test access to members of other classes.
405 # gdb should refuse to print them.
406 # (I feel old -- I remember when this was legal in C -- chastain).
407
408 proc test_wrong_class_members {} {
409 gdb_test "print g_A.b" "There is no member( or method|) named b."
410 gdb_test "print g_B.c" "There is no member( or method|) named c."
411 gdb_test "print g_B.d" "There is no member( or method|) named d."
412 gdb_test "print g_C.b" "There is no member( or method|) named b."
413 gdb_test "print g_C.d" "There is no member( or method|) named d."
414 gdb_test "print g_D.e" "There is no member( or method|) named e."
415 }
416
417 # Test access to names that are not members of any class.
418
419 proc test_nonexistent_members {} {
420 gdb_test "print g_A.y" "There is no member( or method|) named y."
421 gdb_test "print g_B.z" "There is no member( or method|) named z."
422 gdb_test "print g_C.q" "There is no member( or method|) named q."
423 gdb_test "print g_D.p" "There is no member( or method|) named p."
424 }
425
426 # Call a method that expects a base class parameter with base, inherited,
427 # and unrelated class arguments.
428
429 proc test_method_param_class {} {
430 gdb_test "call class_param.Aptr_a (&g_A)" ".* = 1"
431 gdb_test "call class_param.Aptr_x (&g_A)" ".* = 2"
432 gdb_test "call class_param.Aptr_a (&g_B)" ".* = 3"
433 gdb_test "call class_param.Aptr_x (&g_B)" ".* = 4"
434 gdb_test "call class_param.Aref_a (g_A)" ".* = 1"
435 gdb_test "call class_param.Aref_x (g_A)" ".* = 2"
436 gdb_test "call class_param.Aref_a (g_B)" ".* = 3"
437 gdb_test "call class_param.Aref_x (g_B)" ".* = 4"
438 gdb_test "call class_param.Aval_a (g_A)" ".* = 1"
439 gdb_test "call class_param.Aval_x (g_A)" ".* = 2"
440 gdb_test "call class_param.Aval_a (g_B)" ".* = 3"
441 gdb_test "call class_param.Aval_x (g_B)" ".* = 4"
442
443 gdb_test "call class_param.Aptr_a (&foo)" "Cannot resolve .*" "unrelated class *param"
444 gdb_test "call class_param.Aref_a (foo)" "Cannot resolve .*" "unrelated class &param"
445 gdb_test "call class_param.Aval_a (foo)" "Cannot resolve .*" "unrelated class param"
446 }
447
448 # Examine a class with an enum field.
449
450 proc test_enums {} {
451 global gdb_prompt
452 global nl
453
454 # print the object
455
456 # We match the enum values with and without qualifiers. As of
457 # 2008-08-21 we can output the qualifiers for DWARF-2.
458
459 gdb_test "print obj_with_enum" \
460 "\\$\[0-9\]+ = \{priv_enum = (ClassWithEnum::)?red, x = 0\}" \
461 "print obj_with_enum (1)"
462
463 # advance one line
464
465 gdb_test "next" ".*"
466
467 # print the object again
468
469 gdb_test "print obj_with_enum" \
470 "\\$\[0-9\]+ = \{priv_enum = (ClassWithEnum::)?green, x = 0\}" \
471 "print obj_with_enum (2)"
472
473 # print the enum member
474
475 gdb_test "print obj_with_enum.priv_enum" "\\$\[0-9\]+ = (ClassWithEnum::)?green"
476
477 # ptype on the enum member
478
479 gdb_test_multiple "ptype obj_with_enum.priv_enum" "ptype obj_with_enum.priv_enum" {
480 -re "type = enum ClassWithEnum::PrivEnum (: unsigned (int|short|char) )?\{ ?(ClassWithEnum::)?red, (ClassWithEnum::)?green, (ClassWithEnum::)?blue, (ClassWithEnum::)?yellow = 42 ?\}$nl$gdb_prompt $" {
481 pass "ptype obj_with_enum.priv_enum"
482 }
483 -re "type = enum PrivEnum \{ ?(ClassWithEnum::)?red, (ClassWithEnum::)?green, (ClassWithEnum::)?blue, (ClassWithEnum::)?yellow = 42 ?\}$nl$gdb_prompt $" {
484 # gcc 2.95.3 -gdwarf-2
485 # gcc 3.3.2 -gdwarf-2
486 pass "ptype obj_with_enum.priv_enum"
487 }
488 -re "type = enum \{ ?red, green, blue, yellow = 42 ?\}$nl$gdb_prompt $" {
489 # This case case is a little dubious, but it's not clear what
490 # ought to be required of a ptype on a private enum...
491 # -sts 19990324
492 #
493 # It bugs me that this happens with gcc 3.
494 # -- chastain 2003-12-30
495 #
496 # gcc 2.95.3 -gstabs+
497 # gcc 3.3.2 -gstabs+
498 # gcc HEAD 2003-12-28 21:08:30 UTC -gstabs+
499 pass "ptype obj_with_enum.priv_enum"
500 }
501 }
502
503 # ptype on the object
504
505 # NOTE: carlton/2003-02-28: One could certainly argue that plain
506 # "PrivEnum"
507 # is acceptable: PrivEnum is a member of ClassWithEnum, so
508 # there's no need to explicitly qualify its name with
509 # "ClassWithEnum::". The truth, though, is that GDB is simply
510 # forgetting that PrivEnum is a member of ClassWithEnum, so we do
511 # that output for a bad reason instead of a good reason. Under
512 # stabs, we probably can't get this right; under DWARF-2, we can.
513
514 cp_test_ptype_class \
515 "obj_with_enum" "" "class" "ClassWithEnum" \
516 {
517 { field public "ClassWithEnum::PrivEnum priv_enum;" }
518 { field public "int x;" }
519 } \
520 "" \
521 {
522 {
523 "ClassWithEnum::PrivEnum priv_enum;"
524 "PrivEnum priv_enum;"
525 { setup_kfail "gdb/57" "*-*-*" }
526 }
527 }
528
529 # I'll do this test two different ways, because of a parser bug.
530 # See PR gdb/1588.
531
532 gdb_test_multiple "print (ClassWithEnum::PrivEnum) 42" "print (ClassWithEnum::PrivEnum) 42" {
533 -re "\\$\[0-9\]+ = (ClassWithEnum::)?yellow$nl$gdb_prompt $" {
534 pass "print (ClassWithEnum::PrivEnum) 42"
535 }
536 -re "A (parse|syntax) error in expression, near `42'.$nl$gdb_prompt $" {
537 # "parse error" is bison 1.35.
538 # "syntax error" is bison 1.875.
539 kfail "gdb/1588" "print (ClassWithEnum::PrivEnum) 42"
540 }
541 }
542
543 gdb_test_multiple "print ('ClassWithEnum::PrivEnum') 42" "print ('ClassWithEnum::PrivEnum') 42" {
544 -re "\\$\[0-9\]+ = (ClassWithEnum::)?yellow$nl$gdb_prompt $" {
545 # gcc 3.3.2 -gstabs+
546 # gcc HEAD 2003-12-28 21:08:30 UTC -gstabs+
547 pass "print ('ClassWithEnum::PrivEnum') 42"
548 }
549 -re "No symbol \"ClassWithEnum::PrivEnum\" in current context.$nl$gdb_prompt $" {
550 # gcc 2.95.3 -gdwarf-2
551 # gcc 3.3.2 -gdwarf-2
552 # gcc HEAD 2003-12-28 21:08:30 UTC -gdwarf-2
553 # gcc 2.95.3 -gstabs+
554 kfail "gdb/57" "print ('ClassWithEnum::PrivEnum') 42"
555 }
556 }
557 }
558
559 # Pointers to class members
560
561 proc test_pointers_to_class_members {} {
562 gdb_test "print Bar::z" "Cannot reference non-static field \"z\""
563 gdb_test "print &Foo::x" "\\$\[0-9\]+ = &Foo::x"
564 gdb_test "print (int)&Foo::x" "\\$\[0-9\]+ = 0"
565 gdb_test "print (int)&Bar::y == 2*sizeof(int)" "\\$\[0-9\]+ = true"
566
567 gdb_test "ptype Bar::z" "type = int"
568 gdb_test "ptype &Bar::z" "type = int Bar::\\*"
569
570 # TODO: this is a bogus test. It's looking at a variable that
571 # has not even been declared yet, so it's accessing random junk
572 # on the stack and comparing that it's NOT equal to a specific
573 # value. It's been like this since gdb 4.10 in 1993!
574 # -- chastain 2004-01-01
575 gdb_test "print (int)pmi == sizeof(int)" ".* = false"
576 }
577
578 # Test static members.
579
580 proc test_static_members {} {
581 global hex
582
583 gdb_test "print Foo::st" "\\$\[0-9\]+ = 100"
584 gdb_test_no_output "set foo.st = 200" ""
585 gdb_test "print bar.st" "\\$\[0-9\]+ = 200"
586 gdb_test "print &foo.st" "\\$\[0-9\]+ = \\(int ?\\*\\) $hex <Foo::st>"
587 gdb_test "print &Bar::st" "\\$\[0-9\]+ = \\(int ?\\*\\) $hex <Foo::st>"
588 gdb_test "print *\$" "\\$\[0-9\]+ = 200"
589
590 gdb_test_no_output "set print static-members off"
591 gdb_test "print csi" \
592 "{x = 10, y = 20}" \
593 "print csi without static members"
594 gdb_test "print cnsi" \
595 "{x = 30, y = 40}" \
596 "print cnsi without static members"
597
598 gdb_test_no_output "set print static-members on"
599 gdb_test "print csi" \
600 "{x = 10, y = 20, static null = {x = 0, y = 0, static null = <same as static member of an already seen type>}}" \
601 "print csi with static members"
602 gdb_test "print cnsi" \
603 "{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>}" \
604 "print cnsi with static members"
605
606 # Another case of infinite recursion.
607 gdb_test "print Outer::instance" \
608 "{inner = {static instance = {static instance = <same as static member of an already seen type>}}, static instance = {inner = {static instance = {static instance = <same as static member of an already seen type>}}, static instance = <same as static member of an already seen type>}}" \
609 "print recursive static member"
610 }
611
612 proc do_tests {} {
613 global gdb_prompt
614 global nl
615
616
617 gdb_test_no_output "set language c++" ""
618 gdb_test_no_output "set width 0" ""
619
620 if ![runto_main ] then {
621 perror "couldn't run to breakpoint"
622 return
623 }
624
625 gdb_breakpoint inheritance2
626 gdb_test "continue" ".*Breakpoint .* inheritance2.*" ""
627
628 test_ptype_class_objects
629 test_non_inherited_member_access
630 test_wrong_class_members
631 test_nonexistent_members
632 test_method_param_class
633
634 gdb_breakpoint enums2
635 gdb_test "continue" ".*Breakpoint .* enums2.*" "continue to enums2(\\(\\)|)"
636 # Leave enums2. Make sure we reach the next line, in case there
637 # are any more instructions to finish the function call.
638 gdb_test_multiple "finish" "" {
639 -re "enums2 \\(\\);.*$gdb_prompt $" {
640 gdb_test "next" ".*" ""
641 }
642 -re "$gdb_prompt $" { }
643 }
644 test_enums
645
646 gdb_test "finish" ".*" ""
647 test_pointers_to_class_members
648 test_static_members
649
650 # Now some random tests that were just thrown in here.
651
652 gdb_test "print base1::Base1" "<.*Base1.*>" "print ctor of typedef class"
653 gdb_test "print base1::~Base1" "<.*~Base1(\\(\\))?>" \
654 "print dtor of typedef class"
655
656 gdb_test "list ByAnyOtherName::times" ".*int Foo::times.*"
657 }
658
659 do_tests
This page took 0.043288 seconds and 4 git commands to generate.