OSDN Git Service

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