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