1 # Copyright 1998-2025 Free Software Foundation, Inc. 2 3 # This program is free software; you can redistribute it and/or modify 4 # it under the terms of the GNU General Public License as published by 5 # the Free Software Foundation; either version 3 of the License, or 6 # (at your option) any later version. 7 # 8 # This program is distributed in the hope that it will be useful, 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 # GNU General Public License for more details. 12 # 13 # You should have received a copy of the GNU General Public License 14 # along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 # written by Elena Zannoni (ezannoni (at) cygnus.com) 17 # Rewritten by Michael Chastain <mec.gnu (at) mindspring.com> 18 19 # This file is part of the gdb testsuite 20 21 # Tests for overloaded member functions. 22 23 set ws "\[\r\n\t \]+" 24 set nl "\[\r\n\]+" 25 26 27 require allow_cplus_tests 28 29 standard_testfile .cc 30 31 if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} { 32 return -1 33 } 34 35 # Set it up at a breakpoint so we can play with the variable values. 36 37 if {![runto 'marker1']} { 38 continue 39 } 40 41 # Prevent symbol on address 0x0 being printed. 42 gdb_test_no_output "set print symbol off" 43 44 gdb_test "up" ".*main.*" "up from marker1" 45 46 # Print the monster class type. 47 # See virtfunc.exp for a discussion of ptype. 48 # 49 # This is hairy to begin with. It is even more hairy because of the 50 # XX_* alternate patterns to catch the KFAIL and XFAIL cases. 51 52 set re_class "((struct|class) foo \{${ws}public:|struct foo \{)" 53 set re_fields "int ifoo;${ws}const char ?\\* ?ccpfoo;" 54 set XX_fields "int ifoo;${ws}char ?\\* ?ccpfoo;" 55 set re_ctor "foo\\(int\\);${ws}foo\\(int, (char const|const char) ?\\*\\);${ws}foo\\(foo ?&\\);" 56 set re_dtor "~foo\\((void|)\\);" 57 set XX_dtor "~foo\\(int\\);" 58 set re_methods "void foofunc\\(int\\);" 59 set re_methods "${re_methods}${ws}void foofunc\\(int, signed char ?\\*\\);" 60 set re_methods "${re_methods}${ws}int overload1arg\\((void|)\\);" 61 set re_methods "${re_methods}${ws}int overload1arg\\(char\\);" 62 set re_methods "${re_methods}${ws}int overload1arg\\(signed char\\);" 63 set re_methods "${re_methods}${ws}int overload1arg\\(unsigned char\\);" 64 set re_methods "${re_methods}${ws}int overload1arg\\(short( int)?\\);" 65 set re_methods "${re_methods}${ws}int overload1arg\\((unsigned short|short unsigned)( int)?\\);" 66 set re_methods "${re_methods}${ws}int overload1arg\\(int\\);" 67 set re_methods "${re_methods}${ws}int overload1arg\\(unsigned int\\);" 68 set re_methods "${re_methods}${ws}int overload1arg\\(long( int)?\\);" 69 set re_methods "${re_methods}${ws}int overload1arg\\((unsigned long|long unsigned)( int)?\\);" 70 set re_methods "${re_methods}${ws}int overload1arg\\(float\\);" 71 set re_methods "${re_methods}${ws}int overload1arg\\(double\\);" 72 set re_methods "${re_methods}${ws}int overload1arg\\(int \\*\\);" 73 set re_methods "${re_methods}${ws}int overload1arg\\(void \\*\\);" 74 set re_methods "${re_methods}${ws}int overloadfnarg\\((void|)\\);" 75 set re_methods "${re_methods}${ws}int overloadfnarg\\(int\\);" 76 set re_methods "${re_methods}${ws}int overloadfnarg\\(int, int ?\\(\\*\\) ?\\(int\\)\\);" 77 set re_methods "${re_methods}${ws}int overloadargs\\(int\\);" 78 set re_methods "${re_methods}${ws}int overloadargs\\(int, int\\);" 79 set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int\\);" 80 set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int\\);" 81 set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int\\);" 82 set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int\\);" 83 set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int\\);" 84 set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int, int\\);" 85 set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int, int, int\\);" 86 set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int, int, int, int\\);" 87 set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int, int, int, int, int\\);" 88 set re_synth "foo & operator=\\(foo const ?&\\);" 89 90 gdb_test "print foo_instance1" "\\$\[0-9\]+ = \{ifoo = 111, ccpfoo = 0x0\}" 91 92 set lines [gdb_get_lines "ptype foo_instance1"] 93 set test "ptype foo_instance1 output" 94 if { [regexp "^type = $re_class${ws}$XX_fields${ws}$re_synth${ws}$re_dtor${ws}$re_ctor${ws}$re_methods$nl\}\r\n$" $lines] } { 95 # gcc 2.95.3 -gstabs+, no "const" on "const char *" 96 # TODO: gdb.base/constvar.exp has XFAILed this kind of problem for a 97 # long time, but an XFAIL really needs an external bug report. 98 # -- chastain 2003-12-31 99 # setup_xfail "*-*-*" 100 # fail "ptype foo_instance1" 101 # TODO: this should be a KFAIL. 102 pass "$test (shorter match)" 103 } elseif { [regexp "^type = $re_class${ws}$re_fields${ws}$re_synth${ws}$re_dtor${ws}$re_ctor${ws}$re_methods$nl\}\r\n$" $lines] } { 104 # gcc 2.95.3 -gstabs+ if "const char *" ever gets fixed 105 pass "$test" 106 } elseif { [regexp "^type = $re_class${ws}$re_fields${ws}$re_ctor${ws}$XX_dtor${ws}$re_methods$nl\}\r\n$" $lines] } { 107 # gcc 3.3.2 -gdwarf-2, "~foo(int)" 108 # TODO: kfail this 109 # kfail "gdb/1113" "ptype foo_instance1" 110 pass "$test (shorter match)" 111 } elseif { [regexp "^type = $re_class${ws}$re_fields${ws}$re_ctor${ws}$re_dtor${ws}$re_methods$nl\}\r\n$" $lines] } { 112 # gcc 3.3.2 -gdwarf-2, if the dtor bug gets fixed 113 # gcc HEAD -gdwarf-2 (abi-2) 114 # TODO: just pass this 115 pass "$test (shorter match)" 116 } elseif { [regexp "^type = $re_class${ws}$re_fields${ws}$re_synth${ws}$re_ctor${ws}$re_dtor${ws}$re_methods$nl\}\r\n$" $lines] } { 117 # gcc 3.3.2 -gstabs+ 118 # TODO: enough with the "shorter match" 119 pass "$test (shorter match)" 120 } elseif { [regexp "^type = $re_class${ws}$re_fields${ws}$re_ctor${ws}$re_dtor${ws}$re_methods${ws}$re_synth$nl\}\r\n$" $lines] } { 121 # gcc HEAD -gstabs+ (abi-2) 122 pass "$test (shorter match)" 123 } else { 124 fail $test 125 } 126 127 # Print variables and method calls. 128 # This is a walk in the park. 129 130 gdb_test "print foo_instance2" "\\$\[0-9\]+ = \{ifoo = 222, ccpfoo = $hex \"A\"\}" 131 gdb_test "print foo_instance3" "\\$\[0-9\]+ = \{ifoo = 222, ccpfoo = $hex \"A\"\}" 132 133 gdb_test "print foo_instance1.overloadargs(1)" "\\$\[0-9\]+ = 1" \ 134 "print call overloaded func 1 arg" 135 136 # Regression test for overloading with function pointer type. 137 gdb_test "print foo_instance1.overloadfnarg(23, intintfunc)" " = 23" 138 139 # If GDB fails to restore the selected frame properly after the 140 # inferior function call above (see GDB PR 1155 for an explanation of 141 # why this might happen), all the subsequent tests will fail. We 142 # should detect and report that failure, but let the marker call 143 # finish so that the rest of the tests can run undisturbed. 144 145 gdb_test_multiple "frame" "re-selected 'main' frame after inferior call" { 146 -re "#0 marker1.*$gdb_prompt $" { 147 setup_kfail "gdb/1155" s390-*-linux-gnu 148 fail "re-selected 'main' frame after inferior call" 149 gdb_test "finish" ".*main.*at .*overload.cc:.*// marker1-returns-here.*" \ 150 "finish call to marker1" 151 } 152 -re "#1 ($hex in )?main.*$gdb_prompt $" { 153 pass "re-selected 'main' frame after inferior call" 154 } 155 } 156 157 gdb_test "print foo_instance1.overloadargs(1, 2)" \ 158 "\\$\[0-9\]+ = 2" \ 159 "print call overloaded func 2 args" 160 161 gdb_test "print foo_instance1.overloadargs(1, 2, 3)" \ 162 "\\$\[0-9\]+ = 3" \ 163 "print call overloaded func 3 args" 164 165 gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4)" \ 166 "\\$\[0-9\]+ = 4" \ 167 "print call overloaded func 4 args" 168 169 gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5)" \ 170 "\\$\[0-9\]+ = 5" \ 171 "print call overloaded func 5 args" 172 173 gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6)" \ 174 "\\$\[0-9\]+ = 6" \ 175 "print call overloaded func 6 args" 176 177 gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7)" \ 178 "\\$\[0-9\]+ = 7" \ 179 "print call overloaded func 7 args" 180 181 gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7, 8)" \ 182 "\\$\[0-9\]+ = 8" \ 183 "print call overloaded func 8 args" 184 185 gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7, 8, 9)" \ 186 "\\$\[0-9\]+ = 9" \ 187 "print call overloaded func 9 args" 188 189 gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)" \ 190 "\\$\[0-9\]+ = 10" \ 191 "print call overloaded func 10 args" 192 193 gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)" \ 194 "\\$\[0-9\]+ = 11" \ 195 "print call overloaded func 11 args" 196 197 gdb_test "print foo_instance1.overload1arg()" \ 198 "\\$\[0-9\]+ = 1" \ 199 "print call overloaded func void arg" 200 201 gdb_test "print foo_instance1.overload1arg((char)arg2)" \ 202 "\\$\[0-9\]+ = 2" \ 203 "print call overloaded func char arg" 204 205 gdb_test "print foo_instance1.overload1arg((signed char)arg3)" \ 206 "\\$\[0-9\]+ = 3" \ 207 "print call overloaded func signed char arg" 208 209 gdb_test "print foo_instance1.overload1arg((unsigned char)arg4)" \ 210 "\\$\[0-9\]+ = 4" \ 211 "print call overloaded func unsigned char arg" 212 213 gdb_test "print foo_instance1.overload1arg((short)arg5)" \ 214 "\\$\[0-9\]+ = 5" \ 215 "print call overloaded func short arg" 216 217 gdb_test "print foo_instance1.overload1arg((unsigned short)arg6)" \ 218 "\\$\[0-9\]+ = 6" \ 219 "print call overloaded func unsigned short arg" 220 221 gdb_test "print foo_instance1.overload1arg((int)arg7)" \ 222 "\\$\[0-9\]+ = 7" \ 223 "print call overloaded func int arg" 224 225 gdb_test "print foo_instance1.overload1arg((unsigned int)arg8)" \ 226 "\\$\[0-9\]+ = 8" \ 227 "print call overloaded func unsigned int arg" 228 229 gdb_test "print foo_instance1.overload1arg((long)arg9)" \ 230 "\\$\[0-9\]+ = 9" \ 231 "print call overloaded func long arg" 232 233 gdb_test "print foo_instance1.overload1arg((unsigned long)arg10)" \ 234 "\\$\[0-9\]+ = 10" \ 235 "print call overloaded func unsigned long arg" 236 237 gdb_test "print foo_instance1.overload1arg((float)arg11)" \ 238 "\\$\[0-9\]+ = 11" \ 239 "print call overloaded func float arg" 240 241 gdb_test "print foo_instance1.overload1arg((double)arg12)" \ 242 "\\$\[0-9\]+ = 12" \ 243 "print call overloaded func double arg" 244 245 gdb_test "print foo_instance1.overload1arg(&arg13)" \ 246 "\\$\[0-9\]+ = 13" \ 247 "print call overloaded func int\\* arg" 248 249 gdb_test "print foo_instance1.overload1arg(&arg14)" \ 250 "\\$\[0-9\]+ = 14" \ 251 "print call overloaded func char\\* arg" 252 253 gdb_test "print bar(a)" "= 11" 254 gdb_test "print bar(b)" "= 22" 255 gdb_test "print bar(c)" "= 22" 256 gdb_test "print bar(d)" "= 22" 257 258 # PR c++/28901 - gdb thought this was ambiguous. 259 gdb_test "print bar2(e, b)" " = 33" 260 261 # --- 262 263 # List overloaded functions. 264 265 gdb_test_no_output "set listsize 1" "" 266 267 # Build source listing pattern based on an inclusive line range. 268 269 proc line_range_pattern { range_start range_end } { 270 global line_re 271 272 for {set i $range_start} {$i <= $range_end} {incr i} { 273 append pattern "\r\n$i\[ \t\]\[^\r\n\]*" 274 } 275 276 verbose -log "pattern $pattern" 277 return $pattern 278 } 279 280 # The void case is tricky because some compilers say "(void)" 281 # and some compilers say "()". 282 283 gdb_test_multiple "info func overloadfnarg" "list overloaded function with no args" { 284 -re ".*overloadfnarg\\(void\\).*$gdb_prompt $" { 285 # gcc 2 286 gdb_test "list foo::overloadfnarg(void)"\ 287 ".*int foo::overloadfnarg.*\\(void\\).*" \ 288 "list overloaded function with no args" 289 } 290 -re ".*overloadfnarg\\(\\).*$gdb_prompt $" { 291 # gcc 3 292 gdb_test "list foo::overloadfnarg()"\ 293 ".*int foo::overloadfnarg.*\\(void\\).*" \ 294 "list overloaded function with no args" 295 } 296 } 297 298 gdb_test "list foo::overloadfnarg(int)" \ 299 "int foo::overloadfnarg.*\\(int arg\\).*" \ 300 "list overloaded function with int arg" 301 302 gdb_test "list foo::overloadfnarg(int, int (*)(int))" \ 303 "int foo::overloadfnarg.*\\(int arg, int \\(\\*foo\\) \\(int\\)\\).*" \ 304 "list overloaded function with function ptr args" 305 306 gdb_test "list \"foo::overloadfnarg(int, int (*)(int))\"" \ 307 "int foo::overloadfnarg.*\\(int arg, int \\(\\*foo\\) \\(int\\)\\).*" \ 308 "list overloaded function with function ptr args - quotes around argument" 309 310 # Test list with filename. 311 312 gdb_test "list ${srcfile}:intToChar" "int intToChar.*" 313 gdb_test "list ${srcfile}:intToChar(char)" "int intToChar.*" 314 gdb_test "list ${srcfile}:'intToChar(char)'" "int intToChar.*" 315 gdb_test "list '${srcfile}:intToChar(char)'" "int intToChar.*" 316 gdb_test "list '${srcfile}':intToChar(char)" "int intToChar.*" 317 gdb_test "list '${srcfile}':'intToChar(char)'" "int intToChar.*" 318 319 # And with filename and namespace. 320 321 gdb_test "list ${srcfile}:foo::overloadfnarg(int)" "int foo::overloadfnarg.*}" 322 gdb_test "list ${srcfile}:'foo::overloadfnarg(int)'" "int foo::overloadfnarg.*}" 323 324 # Now some tests to see how overloading and namespaces interact. 325 326 gdb_test "print overloadNamespace(1)" ".\[0-9\]* = 1" 327 gdb_test "print overloadNamespace('a')" ".\[0-9\]* = 1" 328 gdb_test "print overloadNamespace(dummyInstance)" ".\[0-9\]* = 2" 329 330 # Static methods. 331 gdb_test "print K::staticoverload ()" " = 1" 332 gdb_test "print K::staticoverload (2)" " = 2" 333 gdb_test "print K::staticoverload (2, 3)" " = 5" 334 335 # Namespace-qualified functions. 336 gdb_test "print N::nsoverload ()" " = 1" 337 gdb_test "print N::nsoverload (2)" " = 2" 338 gdb_test "print N::nsoverload (2, 3)" " = 5" 339 340 # Test "list function" when there are multiple "function" overloads. 341 342 with_test_prefix "list all overloads" { 343 # Bump up listsize again, to make sure the number of lines to 344 # display before/after each location is computed correctly. 345 gdb_test_no_output "set listsize 10" 346 347 set line_bar_A [gdb_get_line_number "int bar (A)"] 348 set line_bar_B [gdb_get_line_number "int bar (B)"] 349 set lines1 [line_range_pattern [expr $line_bar_A - 5] [expr $line_bar_A + 4]] 350 set lines2 [line_range_pattern [expr $line_bar_B - 5] [expr $line_bar_B + 4]] 351 352 set any "\[^\r\n\]*" 353 set h1_re "file: \"${any}overload.cc\", line number: $line_bar_A, symbol: \"bar\\(A\\)\"" 354 set h2_re "file: \"${any}overload.cc\", line number: $line_bar_B, symbol: \"bar\\(B\\)\"" 355 gdb_test "list bar" "${h1_re}${lines1}\r\n${h2_re}${lines2}" 356 } 357 358 if {![runto 'XXX::marker2']} { 359 continue 360 } 361 362 gdb_test "print overloadNamespace(1)" ".\[0-9\]* = 3" "print overloadNamespace(1) in XXX" 363 gdb_test "print overloadNamespace('a')" ".\[0-9\]* = 3" "print overloadNamespace('a') in XXX" 364 gdb_test "print overloadNamespace(dummyInstance)" ".\[0-9\]* = 2" "print overloadNamespace(dummyInstance) in XXX" 365 366 # One last mysterious test. 367 # I wonder what this is for? 368 369 gdb_test "print intToChar(1)" "\\$\[0-9\]+ = 297" 370 371 # Test expression evaluation with overloaded methods 372 gdb_test "print foo::overload1arg" \ 373 "non-unique member `overload1arg' requires type instantiation" 374 375 gdb_test "print foo::overload1arg(char***)" \ 376 "no member function matches that type instantiation" 377 378 gdb_test "print foo::overload1arg(void)" \ 379 "\\$$decimal = {int \\(foo \\*( const|)\\)} $hex <foo::overload1arg\\(\\)>" 380 381 foreach t [list char "signed char" "unsigned char" "short" \ 382 "unsigned short" int "unsigned int" long "unsigned long" \ 383 float double] { 384 gdb_test "print foo::overload1arg($t)" \ 385 "\\$$decimal = {int \\(foo \\*( const|), $t\\)} $hex <foo::overload1arg\\($t\\)>" 386 } 387