]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.cp/classes.exp
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.cp / classes.exp
1 # Copyright 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2 # 2004, 2006, 2007 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 untested classes.exp
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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 "ptype 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 }
320
321 # Test simple access to class members.
322
323 proc test_non_inherited_member_access {} {
324
325 # Print non-inherited members of g_A.
326 gdb_test "print g_A.a" ".* = 1"
327 gdb_test "print g_A.x" ".* = 2"
328
329 # Print non-inherited members of g_B.
330 gdb_test "print g_B.b" ".* = 5"
331 gdb_test "print g_B.x" ".* = 6"
332
333 # Print non-inherited members of g_C.
334 gdb_test "print g_C.c" ".* = 9"
335 gdb_test "print g_C.x" ".* = 10"
336
337 # Print non-inherited members of g_D.
338 gdb_test "print g_D.d" ".* = 19"
339 gdb_test "print g_D.x" ".* = 20"
340
341 # Print non-inherited members of g_E.
342 gdb_test "print g_E.e" ".* = 31"
343 gdb_test "print g_E.x" ".* = 32"
344 }
345
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).
349
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."
357 }
358
359 # Test access to names that are not members of any class.
360
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."
366 }
367
368 # Call a method that expects a base class parameter with base, inherited,
369 # and unrelated class arguments.
370
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"
384
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 &param"
387 gdb_test "call class_param.Aval_a (foo)" "Cannot resolve .*" "unrelated class param"
388 }
389
390 # Examine a class with an enum field.
391
392 proc test_enums {} {
393 global gdb_prompt
394 global nl
395
396 # print the object
397
398 gdb_test "print obj_with_enum" \
399 "\\$\[0-9\]+ = \{priv_enum = red, x = 0\}" \
400 "print obj_with_enum (1)"
401
402 # advance one line
403
404 gdb_test "next" ""
405
406 # print the object again
407
408 gdb_test "print obj_with_enum" \
409 "\\$\[0-9\]+ = \{priv_enum = green, x = 0\}" \
410 "print obj_with_enum (2)"
411
412 # print the enum member
413
414 gdb_test "print obj_with_enum.priv_enum" "\\$\[0-9\]+ = green"
415
416 # ptype on the enum member
417
418 gdb_test_multiple "ptype obj_with_enum.priv_enum" "ptype obj_with_enum.priv_enum" {
419 -re "type = enum ClassWithEnum::PrivEnum \{ ?red, green, blue, yellow = 42 ?\}$nl$gdb_prompt $" {
420 pass "ptype obj_with_enum.priv_enum"
421 }
422 -re "type = enum PrivEnum \{ ?red, green, blue, yellow = 42 ?\}$nl$gdb_prompt $" {
423 # gcc 2.95.3 -gdwarf-2
424 # gcc 3.3.2 -gdwarf-2
425 pass "ptype obj_with_enum.priv_enum"
426 }
427 -re "type = enum \{ ?red, green, blue, yellow = 42 ?\}$nl$gdb_prompt $" {
428 # This case case is a little dubious, but it's not clear what
429 # ought to be required of a ptype on a private enum...
430 # -sts 19990324
431 #
432 # It bugs me that this happens with gcc 3.
433 # -- chastain 2003-12-30
434 #
435 # gcc 2.95.3 -gstabs+
436 # gcc 3.3.2 -gstabs+
437 # gcc HEAD 2003-12-28 21:08:30 UTC -gstabs+
438 pass "ptype obj_with_enum.priv_enum"
439 }
440 }
441
442 # ptype on the object
443
444 # NOTE: carlton/2003-02-28: One could certainly argue that plain
445 # "PrivEnum"
446 # is acceptable: PrivEnum is a member of ClassWithEnum, so
447 # there's no need to explicitly qualify its name with
448 # "ClassWithEnum::". The truth, though, is that GDB is simply
449 # forgetting that PrivEnum is a member of ClassWithEnum, so we do
450 # that output for a bad reason instead of a good reason. Under
451 # stabs, we probably can't get this right; under DWARF-2, we can.
452
453 cp_test_ptype_class \
454 "ptype obj_with_enum" "" "class" "ClassWithEnum" \
455 {
456 { field public "ClassWithEnum::PrivEnum priv_enum;" }
457 { field public "int x;" }
458 } \
459 "" \
460 {
461 {
462 "ClassWithEnum::PrivEnum priv_enum;"
463 "PrivEnum priv_enum;"
464 { setup_kfail "gdb/57" "*-*-*" }
465 }
466 }
467
468 # I'll do this test two different ways, because of a parser bug.
469 # See PR gdb/1588.
470
471 gdb_test_multiple "print (ClassWithEnum::PrivEnum) 42" "print (ClassWithEnum::PrivEnum) 42" {
472 -re "\\$\[0-9\]+ = yellow$nl$gdb_prompt $" {
473 pass "print (ClassWithEnum::PrivEnum) 42"
474 }
475 -re "A (parse|syntax) error in expression, near `42'.$nl$gdb_prompt $" {
476 # "parse error" is bison 1.35.
477 # "syntax error" is bison 1.875.
478 kfail "gdb/1588" "print (ClassWithEnum::PrivEnum) 42"
479 }
480 }
481
482 gdb_test_multiple "print ('ClassWithEnum::PrivEnum') 42" "print ('ClassWithEnum::PrivEnum') 42" {
483 -re "\\$\[0-9\]+ = yellow$nl$gdb_prompt $" {
484 # gcc 3.3.2 -gstabs+
485 # gcc HEAD 2003-12-28 21:08:30 UTC -gstabs+
486 pass "print ('ClassWithEnum::PrivEnum') 42"
487 }
488 -re "No symbol \"ClassWithEnum::PrivEnum\" in current context.$nl$gdb_prompt $" {
489 # gcc 2.95.3 -gdwarf-2
490 # gcc 3.3.2 -gdwarf-2
491 # gcc HEAD 2003-12-28 21:08:30 UTC -gdwarf-2
492 # gcc 2.95.3 -gstabs+
493 kfail "gdb/57" "print ('ClassWithEnum::PrivEnum') 42"
494 }
495 }
496 }
497
498 # Pointers to class members
499
500 proc test_pointers_to_class_members {} {
501 gdb_test "print Bar::z" "Cannot reference non-static field \"z\""
502 gdb_test "print &Foo::x" "\\$\[0-9\]+ = &Foo::x"
503 gdb_test "print (int)&Foo::x" "\\$\[0-9\]+ = 0"
504 gdb_test "print (int)&Bar::y == 2*sizeof(int)" "\\$\[0-9\]+ = true"
505
506 gdb_test "ptype Bar::z" "type = int"
507 gdb_test "ptype &Bar::z" "type = int Bar::\\*"
508
509 # TODO: this is a bogus test. It's looking at a variable that
510 # has not even been declared yet, so it's accessing random junk
511 # on the stack and comparing that it's NOT equal to a specific
512 # value. It's been like this since gdb 4.10 in 1993!
513 # -- chastain 2004-01-01
514 gdb_test "print (int)pmi == sizeof(int)" ".* = false"
515 }
516
517 # Test static members.
518
519 proc test_static_members {} {
520 global hex
521
522 gdb_test "print Foo::st" "\\$\[0-9\]+ = 100"
523 gdb_test "set foo.st = 200" "" ""
524 gdb_test "print bar.st" "\\$\[0-9\]+ = 200"
525 gdb_test "print &foo.st" "\\$\[0-9\]+ = \\(int ?\\*\\) $hex"
526 gdb_test "print &Bar::st" "\\$\[0-9\]+ = \\(int ?\\*\\) $hex"
527 gdb_test "print *\$" "\\$\[0-9\]+ = 200"
528
529 gdb_test "set print static-members off" ""
530 gdb_test "print csi" \
531 "{x = 10, y = 20}" \
532 "print csi without static members"
533 gdb_test "print cnsi" \
534 "{x = 30, y = 40}" \
535 "print cnsi without static members"
536
537 gdb_test "set print static-members on" ""
538 gdb_test "print csi" \
539 "{x = 10, y = 20, static null = {x = 0, y = 0, static null = <same as static member of an already seen type>}}" \
540 "print csi with static members"
541 gdb_test "print cnsi" \
542 "{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>}" \
543 "print cnsi with static members"
544 }
545
546 proc do_tests {} {
547 global prms_id
548 global bug_id
549 global subdir
550 global objdir
551 global srcdir
552 global binfile
553 global gdb_prompt
554 global nl
555
556 set prms_id 0
557 set bug_id 0
558
559 # Start with a fresh gdb.
560
561 gdb_exit
562 gdb_start
563 gdb_reinitialize_dir $srcdir/$subdir
564 gdb_load $binfile
565
566 gdb_test "set language c++" "" ""
567 gdb_test "set width 0" "" ""
568
569 if ![runto_main ] then {
570 perror "couldn't run to breakpoint"
571 return
572 }
573
574 gdb_breakpoint inheritance2
575 gdb_test "continue" ".*Breakpoint .* inheritance2.*" ""
576
577 test_ptype_class_objects
578 test_non_inherited_member_access
579 test_wrong_class_members
580 test_nonexistent_members
581 test_method_param_class
582
583 gdb_breakpoint enums2
584 gdb_test "continue" ".*Breakpoint .* enums2.*" "continue to enums2(\\(\\)|)"
585 gdb_test "finish" "" ""
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
628 do_tests