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