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