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