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