a625dba9aad0f1f8b209742be011aa178561c71b
[external/binutils.git] / gdb / testsuite / lib / cp-support.exp
1 # This test code is part of GDB, the GNU debugger.
2
3 # Copyright 2003-2016 Free Software Foundation, Inc.
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 # Auxiliary function to check for known problems.
19 #
20 # EXPECTED_STRING is the string expected by the test.
21 #
22 # ACTUAL_STRING is the actual string output by gdb.
23 #
24 # ERRATA_TABLE is a list of lines of the form:
25 #
26 #  { expected-string broken-string {eval-block} }
27 #
28 # If there is a line for the given EXPECTED_STRING, and if the
29 # ACTUAL_STRING output by gdb is the same as the BROKEN_STRING in the
30 # table, then I eval the eval-block.
31
32 proc cp_check_errata { expected_string actual_string errata_table } {
33     foreach erratum $errata_table {
34         if { "$expected_string" == [lindex $erratum 0]
35         &&   "$actual_string"   == [lindex $erratum 1] } then {
36             eval [lindex $erratum 2]
37         }
38     }
39 }
40
41 # Test ptype of a class.
42 #
43 # Different C++ compilers produce different output.  To accommodate all
44 # the variations listed below, I read the output of "ptype" and process
45 # each line, matching it to the class description given in the
46 # parameters.
47 #
48 # IN_EXP is the expression to use; the appropriate "ptype" invocation
49 # is prepended to it.  IN_TESTNAME is the testname for
50 # gdb_test_multiple.  If IN_TESTNAME is the empty string, then it
51 # defaults to "ptype IN_EXP".
52 #
53 # IN_KEY is "class" or "struct".  For now, I ignore it, and allow either
54 # "class" or "struct" in the output, as long as the access specifiers all
55 # work out okay.
56 #
57 # IN_TAG is the class tag or structure tag.
58 #
59 # IN_CLASS_TABLE is a list of class information.  Each entry contains a
60 # keyword and some values.  The keywords and their values are:
61 #
62 #   { base "base-declaration" }
63 #
64 #      the class has a base with the given declaration.
65 #
66 #   { vbase "name" }
67 #
68 #      the class has a virtual base pointer with the given name.  this
69 #      is for gcc 2.95.3, which emits ptype entries for the virtual base
70 #      pointers.  the vbase list includes both indirect and direct
71 #      virtual base classes (indeed, a virtual base is usually
72 #      indirect), so this information cannot be derived from the base
73 #      declarations.
74 #
75 #   { field "access" "declaration" }
76 #
77 #      the class has a data field with the given access type and the
78 #      given declaration.
79 #
80 #   { method "access" "declaration" }
81 #
82 #      the class has a member function with the given access type
83 #      and the given declaration.
84 #
85 #   { typedef "access" "declaration" }
86 #
87 #      the class has a typedef with the given access type and the
88 #      given declaration.
89 #
90 # If you test the same class declaration more than once, you can specify
91 # IN_CLASS_TABLE as "ibid".  "ibid" means: look for a previous class
92 # table that had the same IN_KEY and IN_TAG, and re-use that table.
93 #
94 # IN_TAIL is the expected text after the close brace, specifically the "*"
95 # in "struct { ... } *".  This is an optional parameter.  The default
96 # value is "", for no tail.
97 #
98 # IN_ERRATA_TABLE is a list of errata entries.  See cp_check_errata for the
99 # format of the errata table.  Note: the errata entries are not subject to
100 # demangler syntax adjustment, so you have to make a bigger table
101 # with lines for each output variation.
102
103 # IN_PTYPE_ARG are arguments to pass to ptype.  The default is "/r".
104 #
105 # gdb can vary the output of ptype in several ways:
106 #
107 # . CLASS/STRUCT
108 #
109 #   The output can start with either "class" or "struct", depending on
110 #   what the symbol table reader in gdb decides.  This is usually
111 #   unrelated to the original source code.
112 #
113 #     dwarf-2  debug info distinguishes class/struct, but gdb ignores it
114 #     stabs+   debug info does not distinguish class/struct
115 #     hp       debug info distinguishes class/struct, and gdb honors it
116 #
117 #   I tried to accommodate this with regular expressions such as
118 #   "((class|struct) A \{ public:|struct A \{)", but that turns into a
119 #   hairy mess because of optional private virtual base pointers and
120 #   optional public synthetic operators.  This is the big reason I gave
121 #   up on regular expressions and started parsing the output.
122 #
123 # . REDUNDANT ACCESS SPECIFIER
124 #
125 #   In "class { private: ... }" or "struct { public: ... }", gdb might
126 #   or might not emit a redundant initial access specifier, depending
127 #   on the gcc version.
128 #
129 # . VIRTUAL BASE POINTERS
130 #
131 #   If a class has virtual bases, either direct or indirect, the class
132 #   will have virtual base pointers.  With gcc 2.95.3, gdb prints lines
133 #   for these virtual base pointers.  This does not happen with gcc
134 #   3.3.4, gcc 3.4.1, or hp acc A.03.45.
135 #
136 #   I accept these lines.  These lines are optional; but if I see one of
137 #   these lines, then I expect to see all of them.
138 #
139 #   Note: drow considers printing these lines to be a bug in gdb.
140 #
141 # . SYNTHETIC METHODS
142 #
143 #   A C++ compiler may synthesize some methods: an assignment
144 #   operator, a copy constructor, a constructor, and a destructor.  The
145 #   compiler might include debug information for these methods.
146 #
147 #     dwarf-2  gdb does not show these methods
148 #     stabs+   gdb shows these methods
149 #     hp       gdb does not show these methods
150 #
151 #   I accept these methods.  These lines are optional, and any or
152 #   all of them might appear, mixed in anywhere in the regular methods.
153 #
154 #   With gcc v2, the synthetic copy-ctor and ctor have an additional
155 #   "int" parameter at the beginning, the "in-charge" flag.
156 #
157 # . DEMANGLER SYNTAX VARIATIONS
158 #
159 #   Different demanglers produce "int foo(void)" versus "int foo()",
160 #   "const A&" versus "const A &", and so on.
161 #
162 # TESTED WITH
163 #
164 #   gcc 2.95.3 -gdwarf-2
165 #   gcc 2.95.3 -gstabs+
166 #   gcc 3.3.4 -gdwarf-2
167 #   gcc 3.3.4 -gstabs+
168 #   gcc 3.4.1 -gdwarf-2
169 #   gcc 3.4.1 -gstabs+
170 #   gcc HEAD 20040731 -gdwarf-2
171 #   gcc HEAD 20040731 -gstabs+
172
173 # TODO
174 #
175 # Tagless structs.
176 #
177 # "A*" versus "A *" and "A&" versus "A &" in user methods.
178 #
179 # -- chastain 2004-08-07
180
181 proc cp_test_ptype_class { in_exp in_testname in_key in_tag in_class_table { in_tail "" } { in_errata_table { } } { in_ptype_arg /r } } {
182     global gdb_prompt
183     set wsopt "\[\r\n\t \]*"
184
185     # The test name defaults to the command, but without the
186     # arguments, for historical reasons.
187
188     if { "$in_testname" == "" } then { set in_testname "ptype $in_exp" }
189
190     set in_command "ptype${in_ptype_arg} $in_exp"
191
192     # Save class tables in a history array for reuse.
193
194     global cp_class_table_history
195     if { $in_class_table == "ibid" } then {
196         if { ! [info exists cp_class_table_history("$in_key,$in_tag") ] } then {
197             fail "$in_testname // bad ibid"
198             return
199         }
200         set in_class_table $cp_class_table_history("$in_key,$in_tag")
201     } else {
202         set cp_class_table_history("$in_key,$in_tag") $in_class_table
203     }
204
205     # Split the class table into separate tables.
206
207     set list_bases   { }
208     set list_vbases  { }
209     set list_fields  { }
210     set list_methods { }
211     set list_typedefs { }
212
213     foreach class_line $in_class_table {
214         switch [lindex $class_line 0] {
215             "base"   { lappend list_bases   [lindex $class_line 1] }
216             "vbase"  { lappend list_vbases  [lindex $class_line 1] }
217             "field"  { lappend list_fields  [lrange $class_line 1 2] }
218             "method" { lappend list_methods [lrange $class_line 1 2] }
219             "typedef" { lappend list_typedefs [lrange $class_line 1 2] }
220             default  { fail "$in_testname // bad line in class table: $class_line"; return; }
221         }
222     }
223
224     # Construct a list of synthetic operators.
225     # These are: { count ccess-type regular-expression }.
226
227     set list_synth { }
228     lappend list_synth [list 0 "public" "$in_tag & operator=\\($in_tag const ?&\\);"]
229     lappend list_synth [list 0 "public" "$in_tag\\((int,|) ?$in_tag const ?&\\);"]
230     lappend list_synth [list 0 "public" "$in_tag\\((int|void|)\\);"]
231
232     # Actually do the ptype.
233
234     set parse_okay 0
235     gdb_test_multiple "$in_command" "$in_testname // parse failed" {
236         -re "type = (struct|class)${wsopt}(\[^ \t\]*)${wsopt}(\\\[with .*\\\]${wsopt})?((:\[^\{\]*)?)${wsopt}\{(.*)\}${wsopt}(\[^\r\n\]*)\[\r\n\]+$gdb_prompt $" {
237             set parse_okay          1
238             set actual_key          $expect_out(1,string)
239             set actual_tag          $expect_out(2,string)
240             set actual_base_string  $expect_out(4,string)
241             set actual_body         $expect_out(6,string)
242             set actual_tail         $expect_out(7,string)
243         }
244     }
245     if { ! $parse_okay } then { return }
246
247     # Check the actual key.  It would be nice to require that it match
248     # the input key, but gdb does not support that.  For now, accept any
249     # $actual_key as long as the access property of each field/method
250     # matches.
251
252     switch "$actual_key" {
253         "class"  { set access "private" }
254         "struct" { set access "public"  }
255         default  {
256             cp_check_errata "class"  "$actual_key" $in_errata_table
257             cp_check_errata "struct" "$actual_key" $in_errata_table
258             fail "$in_testname // wrong key: $actual_key"
259             return
260         }
261     }
262
263     # Check the actual tag.
264
265     if { "$actual_tag" != "$in_tag" } then {
266         cp_check_errata "$in_tag" "$actual_tag" $in_errata_table
267         fail "$in_testname // wrong tag: $actual_tag"
268         return
269     }
270
271     # Check the actual bases.
272     # First parse them into a list.
273
274     set list_actual_bases { }
275     if { "$actual_base_string" != "" } then {
276         regsub "^:${wsopt}" $actual_base_string "" actual_base_string
277         set list_actual_bases [split $actual_base_string ","]
278     }
279
280     # Check the base count.
281
282     if { [llength $list_actual_bases] < [llength $list_bases] } then {
283         fail "$in_testname // too few bases"
284         return
285     }
286     if { [llength $list_actual_bases] > [llength $list_bases] } then {
287         fail "$in_testname // too many bases"
288         return
289     }
290
291     # Check each base.
292
293     foreach actual_base $list_actual_bases {
294         set actual_base [string trim $actual_base]
295         set base [lindex $list_bases 0]
296         if { "$actual_base" != "$base" } then {
297             cp_check_errata "$base" "$actual_base" $in_errata_table
298             fail "$in_testname // wrong base: $actual_base"
299             return
300         }
301         set list_bases [lreplace $list_bases 0 0]
302     }
303
304     # Parse each line in the body.
305
306     set last_was_access 0
307     set vbase_match 0
308
309     foreach actual_line [split $actual_body "\r\n"] {
310
311         # Chomp the line.
312
313         set actual_line [string trim $actual_line]
314         if { "$actual_line" == "" } then { continue }
315
316         # Access specifiers.
317
318         if { [regexp "^(public|protected|private)${wsopt}:\$" "$actual_line" s0 s1] } then {
319             set access "$s1"
320             if { $last_was_access } then {
321                 fail "$in_testname // redundant access specifier"
322                 return
323             }
324             set last_was_access 1
325             continue
326         } else {
327             set last_was_access 0
328         }
329
330         # Optional virtual base pointer.
331
332         if { [ llength $list_vbases ] > 0 } then {
333             set vbase [lindex $list_vbases 0]
334             if { [ regexp "$vbase \\*(_vb.|_vb\\\$|__vb_)\[0-9\]*$vbase;" $actual_line ] } then {
335                 if { "$access" != "private" } then {
336                     cp_check_errata "private" "$access" $in_errata_table
337                     fail "$in_testname // wrong access specifier for virtual base: $access"
338                     return
339                 }
340                 set list_vbases [lreplace $list_vbases 0 0]
341                 set vbase_match 1
342                 continue
343             }
344         }
345
346         # Data field.
347
348         if { [llength $list_fields] > 0 } then {
349             set field_access [lindex [lindex $list_fields 0] 0]
350             set field_decl   [lindex [lindex $list_fields 0] 1]
351             if { "$actual_line" == "$field_decl" } then {
352                 if { "$access" != "$field_access" } then {
353                     cp_check_errata "$field_access" "$access" $in_errata_table
354                     fail "$in_testname // wrong access specifier for field: $access"
355                     return
356                 }
357                 set list_fields [lreplace $list_fields 0 0]
358                 continue
359             }
360
361             # Data fields must appear before synths and methods.
362             cp_check_errata "$field_decl" "$actual_line" $in_errata_table
363             fail "$in_testname // unrecognized line type 1: $actual_line"
364             return
365         }
366
367         # Method function.
368
369         if { [llength $list_methods] > 0 } then {
370             set method_access [lindex [lindex $list_methods 0] 0]
371             set method_decl   [lindex [lindex $list_methods 0] 1]
372             if { "$actual_line" == "$method_decl" } then {
373                 if { "$access" != "$method_access" } then {
374                     cp_check_errata "$method_access" "$access" $in_errata_table
375                     fail "$in_testname // wrong access specifier for method: $access"
376                     return
377                 }
378                 set list_methods [lreplace $list_methods 0 0]
379                 continue
380             }
381
382             # gcc 2.95.3 shows "foo()" as "foo(void)".
383             regsub -all "\\(\\)" $method_decl "(void)" method_decl
384             if { "$actual_line" == "$method_decl" } then {
385                 if { "$access" != "$method_access" } then {
386                     cp_check_errata "$method_access" "$access" $in_errata_table
387                     fail "$in_testname // wrong access specifier for method: $access"
388                     return
389                 }
390                 set list_methods [lreplace $list_methods 0 0]
391                 continue
392             }
393         }
394
395         # Typedef
396
397         if {[llength $list_typedefs] > 0} {
398             set typedef_access [lindex [lindex $list_typedefs 0] 0]
399             set typedef_decl [lindex [lindex $list_typedefs 0] 1]
400             if {[string equal $actual_line $typedef_decl]} {
401                 if {![string equal $access $typedef_access]} {
402                     cp_check_errata $typedef_access $access $in_errata_table
403                     fail "$in_testname // wrong access specifier for typedef: $access"
404                     return
405                 }
406                 set list_typedefs [lreplace $list_typedefs 0 0]
407                 continue
408             }
409         }
410
411         # Synthetic operators.  These are optional and can be mixed in
412         # with the methods in any order, but duplicates are wrong.
413         #
414         # This test must come after the user methods, so that a user
415         # method which matches a synth-method pattern is treated
416         # properly as a user method.
417
418         set synth_match 0
419         for { set isynth 0 } { $isynth < [llength $list_synth] } { incr isynth } {
420             set synth         [lindex $list_synth $isynth]
421             set synth_count   [lindex $synth 0]
422             set synth_access  [lindex $synth 1]
423             set synth_re      [lindex $synth 2]
424
425             if { [ regexp "$synth_re" "$actual_line" ] } then {
426
427                 if { "$access" != "$synth_access" } then {
428                     cp_check_errata "$synth_access" "$access" $in_errata_table
429                     fail "$in_testname // wrong access specifier for synthetic operator: $access"
430                     return
431                 }
432
433                 if { $synth_count > 0 } then {
434                     cp_check_errata "$actual_line" "$actual_line" $in_errata_table
435                     fail "$in_testname // duplicate synthetic operator: $actual_line"
436                 }
437
438                 # Update the count in list_synth.
439
440                 incr synth_count 
441                 set synth [list $synth_count $synth_access "$synth_re"]
442                 set list_synth [lreplace $list_synth $isynth $isynth $synth]
443
444                 # Match found.
445
446                 set synth_match 1
447                 break
448             }
449         }
450         if { $synth_match } then { continue }
451
452         # Unrecognized line.
453
454         if { [llength $list_methods] > 0 } then {
455             set method_decl [lindex [lindex $list_methods 0] 1]
456             cp_check_errata "$method_decl" "$actual_line" $in_errata_table
457         }
458
459         fail "$in_testname // unrecognized line type 2: $actual_line"
460         return
461     }
462
463     # Check for missing elements.
464
465     if { $vbase_match } then {
466         if { [llength $list_vbases] > 0 } then {
467             fail "$in_testname // missing virtual base pointers"
468             return
469         }
470     }
471
472     if { [llength $list_fields] > 0 } then {
473         fail "$in_testname // missing fields"
474         return
475     }
476
477     if { [llength $list_methods] > 0 } then {
478         fail "$in_testname // missing methods"
479         return
480     }
481
482     if {[llength $list_typedefs] > 0} {
483         fail "$in_testname // missing typedefs"
484         return
485     }
486
487     # Check the tail.
488
489     set actual_tail [string trim $actual_tail]
490     if { "$actual_tail" != "$in_tail" } then {
491         cp_check_errata "$in_tail" "$actual_tail" $in_errata_table
492         fail "$in_testname // wrong tail: $actual_tail"
493         return
494     }
495
496     # It all worked!
497
498     pass "$in_testname"
499     return
500 }