| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 
 | # Copyright (C) 1998-2023 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# written by Elena Zannoni (ezannoni@cygnus.com)
# modified by Michael Chastain (chastain@redhat.com)
# This file is part of the gdb testsuite
#
# tests for overloaded member functions. Set breakpoints on
# overloaded member functions
#
global timeout
set timeout 15
#
# test running programs
#
if { [skip_cplus_tests] } { return }
standard_testfile .cc
if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
    return -1
}
# set it up at a breakpoint so we can play with the variable values
#
if {![runto_main]} {
    return
}
# When I ask gdb to set a breakpoint on an overloaded function,
# gdb gives me a choice menu.  I might get stuck in that choice menu
# (for example, if C++ name mangling is not working properly).
#
# This procedure issues a command that works at either the menu
# prompt or the command prompt to get back to the command prompt.
#
# Note that an empty line won't do it (it means 'repeat the previous command'
# at top level).  A line with a single space in it works nicely.
proc take_gdb_out_of_choice_menu {} {
    global gdb_prompt
    gdb_test_multiple " " " " {
        -re ".*$gdb_prompt $" {
        }
	timeout {
	    perror "could not resynchronize to command prompt (timeout)"
	    continue
	}
    }
}
# This procedure sets an overloaded breakpoint.  When users ask for
# such a breakpoint, gdb gives a menu of 'cancel' 'all' and one choice
# per overload.  Users can then choose from that menu by number.
#
# NAME is the spec to use to create the breakpoint.  EXPECTEDMENU is
# the expected menu.  MYCHOICE is the choice selected.  Can be more
# than one overload, e.g. "2-3".  BPNUMBER is the expected next
# breakpoint created.  LINENUMBERS is a list of line numbers, one
# element per expected breakpoint created.
proc set_bp_overloaded {name expectedmenu mychoice bpnumber linenumbers} {
    global gdb_prompt hex decimal srcfile
    # Get into the overload menu.
    gdb_test_multiple "break $name" "bp menu for $name choice $mychoice" {
	-re "$expectedmenu" {
	    pass $gdb_test_name
	    set any "\[^\r\n\]*"
	    # True if we've seen a bad breakpoint.
	    set bad_bp 0
	    # How many breakpoints we expect to see.
	    set expected_bps [llength $linenumbers]
	    # The count of seen breakpoints.
	    set seen_bps 0
	    # Choose my choice.
	    gdb_test_multiple "$mychoice" "set bp $bpnumber on $name $mychoice line $linenumbers" {
		-re "Breakpoint ($decimal) at $hex: file$any$srcfile, line ($decimal).\r\n" {
		    set got_num $expect_out(1,string)
		    set got_line $expect_out(2,string)
		    if {$seen_bps >= $expected_bps} {
			set bad_bp 1
		    } else {
			set linenumber [lindex $linenumbers $seen_bps]
			if {$got_num != $bpnumber || $got_line != $linenumber} {
			    set bad_bp 1
			}
			incr bpnumber
			incr seen_bps
		    }
		    exp_continue
		}
		-re "$gdb_prompt $" {
		    gdb_assert {!$bad_bp && $seen_bps == $expected_bps} \
			$gdb_test_name
                }
                timeout {
                    fail "$gdb_test_name (timeout)"
                    take_gdb_out_of_choice_menu
                }
            }
        }
        -re ".*\r\n> " {
            fail "$gdb_test_name (bad menu)"
            take_gdb_out_of_choice_menu
        }
        -re ".*$gdb_prompt $" {
            fail "$gdb_test_name (no menu)"
        }
        timeout {
            fail "$gdb_test_name (timeout)"
            take_gdb_out_of_choice_menu
        }
    }
}
# Compute the expected menu for overload1arg.
# Note the arg type variations for void and integer types.
# This accommodates different versions of g++.
# Probe for the real types.  This will do some unnecessary checking
# for some simple types (like "int"), but it's just easier to loop
# over all_types instead of calling out just the exceptions.
# This list /must/ remain in the same order that the methods are
# called in the source code.  Otherwise the order in which breakpoints
# are hit (tested below) will be incorrect.
set all_types [list void char signed_char unsigned_char short_int \
		   unsigned_short_int int unsigned_int long_int \
		   unsigned_long_int float double]
# ARGUMENTS is an array that will map from synthetic type to argument
# expressions in the source code, which is of the form "arg = $decimal".
# ARGUMENTS stores this decimal number.
array set arguments {
    void ""
    char 2
    signed_char 3
    unsigned_char 4
    short_int 5
    unsigned_short_int 6
    int 7
    unsigned_int 8
    long_int 9
    unsigned_long_int 10
    float 100(.0)?
    double 200(.0)?
}
unset -nocomplain line types
foreach type $all_types {
    # TYPES is an array that maps the synthetic names in ALL_TYPES
    # to the real type used in the debugger.  These will be checked
    # below and changed if the debugger thinks they are different from
    # their default values.
    set types($type) [join [split $type "_"] " "]
    # LINE is an array that will map from synthetic type to line number.
    # in the source code.
    set line($type) [gdb_get_line_number "fo1 $type"]
    # Probe for the actual type.
    gdb_test_multiple "print &foo::overload1arg($types($type))" \
        "probe $types($type)" {
            -re ".*\<foo::.*\>.*$gdb_prompt $" {
	        regexp {<.*>} $expect_out(0,string) func
	        regexp {\(.*\)} $func real_type
	        # Store the real type into TYPES.
	        set types($type) [string trim $real_type {()}]
	        # Create an inverse mapping of the actual type to
	        # the synthetic type.
	        set type_map("$types($type)") $type
	        pass "detect $type"
	    }
    }
}
# This is a list of the actual overloaded method arguments.
set overloads {}
foreach type $all_types {
    lappend overloads $types($type)
}
# Sort this list alphabetically.
set overloads [lsort $overloads]
# Create the menu list.
set items {"cancel" "all"}
foreach ovld $overloads {
    lappend items "$srcfile:foo::overload1arg\\($ovld\\)"
}
set menu_items {}
set idx 0
foreach item $items {
    lappend menu_items ".$idx. .*$item"
    incr idx
}
set menu_overload1arg [join $menu_items {[\r\n]*}]
append menu_overload1arg {[\r\n]*> $}
# Set multiple-symbols to "ask", to allow us to test the use
# of the multiple-choice menu when breaking on an overloaded method.
gdb_test_no_output "set multiple-symbols ask"
# The last breakpoint created.
set bpnum 1
# Set breakpoints on foo::overload1arg, one by one.
set method "foo::overload1arg"
for {set idx 0} {$idx < [llength $overloads]} {incr idx} {
    set type [lindex $overloads $idx]
    set_bp_overloaded $method $menu_overload1arg \
	[expr {$idx + 2}] [incr bpnum] $line($type_map("$type"))
}
# Verify the breakpoints.
set bptable "Num\[\t \]+Type\[\t \]+Disp Enb Address\[\t \]+What.*\[\r\n]+"
append bptable "\[0-9\]+\[\t \]+breakpoint\[\t \]+keep\[\t \]y\[\t \]+$hex\[\t \]+in main(\\((|void)\\))? at.*$srcfile:49\[\r\n\]+"
append bptable "\[\t \]+breakpoint already hit 1 time\[\r\n\]+."
foreach ovld $overloads {
    append bptable [format "\[0-9\]+\[\t \]+breakpoint\[\t \]+keep y\[\t \]+$hex\[\t \]+in foo::overload1arg\\(%s\\) at.*$srcfile:%d\[\r\n\]+" $ovld \
			$line($type_map("$ovld"))]
}
gdb_test "info break" $bptable "breakpoint info (after setting one-by-one)"
# Test choice "cancel".
# This is copy-and-paste from set_bp_overloaded.
send_gdb "break foo::overload1arg\n" 
gdb_expect {
    -re "$menu_overload1arg" {
        pass "bp menu for foo::overload1arg choice cancel"
        # Choose cancel.
        send_gdb "0\n"
        gdb_expect {
            -re "canceled\r\n$gdb_prompt $" {
                pass "set bp on overload1arg canceled"
            }
	    -re "cancelled\r\n$gdb_prompt $" {
		pass "set bp on overload1arg canceled"
	    }
            -re ".*$gdb_prompt $" {
                fail "set bp on overload1arg canceled (bad message)"
            }
            timeout {
                fail "set bp on overload1arg canceled (timeout)"
                take_gdb_out_of_choice_menu
            }
        }
    }
    -re ".*\r\n> " {
        fail "bp menu for foo::overload1arg choice cancel (bad menu)"
        take_gdb_out_of_choice_menu
    }
    -re ".*$gdb_prompt $" {
        fail "bp menu for foo::overload1arg choice cancel (no menu)"
    }
    timeout {
        fail "bp menu for foo::overload1arg choice cancel (timeout)"
        take_gdb_out_of_choice_menu
    }
}
gdb_test "info break" $bptable "breakpoint info (after cancel)"
# Test that if the user selects multiple entries from the option list,
# GDB creates one breakpoint per entry.
with_test_prefix "multiple breakpoints" {
    set method "foo::overload1arg"
    set expected_lines {}
    for {set i 0} {$i < 2} {incr i} {
	set type [lindex $overloads $i]
	lappend expected_lines $line($type_map("$type"))
    }
    set_bp_overloaded $method $menu_overload1arg \
	"2-3" [incr bpnum] $expected_lines
    incr bpnum
}
# Delete these breakpoints.
send_gdb "delete breakpoints\n"
gdb_expect {
    -re "Delete all breakpoints.* $" {
        send_gdb "y\n"
        gdb_expect {
            -re ".*$gdb_prompt $" {
                pass "delete all breakpoints"
            }
            timeout {
                fail "delete all breakpoints (timeout)"
            }
        }
    }
    timeout {
        fail "delete all breakpoints (timeout)"
    }
}
gdb_test "info breakpoints" "No breakpoints or watchpoints." "breakpoint info (after delete)"
# Test choice "all".
# This is copy-and-paste from set_bp_overloaded.
incr bpnum
send_gdb "break foo::overload1arg\n" 
gdb_expect {
    -re "$menu_overload1arg" {
        pass "bp menu for foo::overload1arg choice all"
        # Choose all.
        send_gdb "1\n"
        gdb_expect {
	    -re "Breakpoint $bpnum at $hex: foo::overload1arg. .12 locations.\r\n.*$gdb_prompt $" {
                pass "set bp on overload1arg all"
            }
            -re ".*$gdb_prompt $" {
                fail "set bp on overload1arg all (bad message)"
            }
            timeout {
                fail "set bp on overload1arg all (timeout)"
                take_gdb_out_of_choice_menu
            }
        }
    }
    -re ".*\r\n> " {
        fail "bp menu for foo::overload1arg choice all (bad menu)"
        take_gdb_out_of_choice_menu
    }
    -re ".*$gdb_prompt $" {
        fail "bp menu for foo::overload1arg choice all (no menu)"
    }
    timeout {
        fail "bp menu for foo::overload1arg choice all (timeout)"
        take_gdb_out_of_choice_menu
    }
}
# Create the breakpoint table for "info breakpoint".
set bptable "Num\[\t \]+Type\[\t \]+Disp Enb Address\[\t \]+What.*\[\r\n]+"
append bptable "\[0-9\]+\[\t \]+breakpoint\[\t \]+keep\[\t \]y\[\t \]+<MULTIPLE>.*\[\r\n\]+"
foreach ovld {void char signed_char unsigned_char short_int \
		  unsigned_short_int int unsigned_int long_int \
		  unsigned_long_int float double} {
  append bptable [format "\[0-9\]+.\[0-9\]+\[\t \]+y\[\t \]+$hex\[\t \]+in foo::overload1arg\\(%s\\) at.*$srcfile:%d\[\r\n\]+" \
		      $types($ovld) $line($ovld)]
}
gdb_test "info break" $bptable "breakpoint info (after setting on all)"
# Run through each breakpoint.
proc continue_to_bp_overloaded {bpnumber might_fail line argtype argument} {
    global gdb_prompt hex decimal srcfile bkptno_num_re
    if {$argument == ""} {
        set actuals ""
    } else {
        set actuals "arg=$argument"
        if {[regexp {char} $argtype]} {
	    append actuals " \\'\\\\00$argument\\'"
      }
    }
    if {[string match $argtype "void"]} {
        set body "return $decimal;"
    } else {
        set body "arg = 0; return $decimal;"
    }
    gdb_test_multiple "continue" "continue to bp overloaded : $argtype" {
        -re "Continuing.\r\n\r\nBreakpoint $bkptno_num_re, foo::overload1arg \\(this=${hex}(, )?$actuals\\) at .*$srcfile:$line\r\n$decimal\[\t \]+{ $body }.*$gdb_prompt $" {
            pass "continue to bp overloaded : $argtype"
        }
        -re "Continuing.\r\n\r\nBreakpoint $bkptno_num_re, foo::overload1arg \\(this=${hex}, arg=.*\\) at .*$srcfile:$line\r\n$decimal\[\t \]+{ $body }.*$gdb_prompt $" {
            if $might_kfail {
                kfail "c++/8130" "continue to bp overloaded : $argtype"
            } else {
                fail "continue to bp overloaded : $argtype"
            }
        }
    }
}
# An array which describes which of these methods might be expected
# to kfail on GCC 2.95. See C++/8210.
array set might_fail {
    void 0
    char 1
    signed_char 1
    unsigned_char 1
    short_int 1
    unsigned_short_int 1
    int 0
    unsigned_int 0
    long_int 0
    unsigned_long_int 0
    float 0
    double 1
}
foreach type $all_types {
    continue_to_bp_overloaded $bpnum $might_fail($type) $line($type) \
	$type $arguments($type)
}
# Test breaking on an overloaded function when multiple-symbols
# is set to "cancel"
gdb_test_no_output "set multiple-symbols cancel"
gdb_test "break foo::foofunc" \
    "canceled.*" \
    "break on ambiguous symbol when multiple-symbols is set to cancel"
# Test breaking on an overloaded function when multiple-symbols
# is set to "all"
gdb_test_no_output "set multiple-symbols all"
gdb_test "break foo::foofunc" \
    "Breakpoint \[0-9\]+ at ${hex}: foo::foofunc. .2 locations..*" \
    "break on ambiguous symbol when multiple-symbols is set to all"
# That's all, folks.
unset -nocomplain line types
gdb_continue_to_end "finish program"
 |