1*1424dfb3Schristos# Copyright 2017-2020 Free Software Foundation, Inc.
207163879Schristos
307163879Schristos# This program is free software; you can redistribute it and/or modify
407163879Schristos# it under the terms of the GNU General Public License as published by
507163879Schristos# the Free Software Foundation; either version 3 of the License, or
607163879Schristos# (at your option) any later version.
707163879Schristos#
807163879Schristos# This program is distributed in the hope that it will be useful,
907163879Schristos# but WITHOUT ANY WARRANTY; without even the implied warranty of
1007163879Schristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1107163879Schristos# GNU General Public License for more details.
1207163879Schristos#
1307163879Schristos# You should have received a copy of the GNU General Public License
1407163879Schristos# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1507163879Schristos
1607163879Schristos# Tests for function local static variables, both C and C++.
1707163879Schristos
1807163879Schristos# This file is part of the gdb testsuite.
1907163879Schristos
2007163879Schristosstandard_testfile .c
2107163879Schristos
2207163879Schristos# A few expected errors.
2307163879Schristosset syntax_re "A syntax error in expression, near.*"
2407163879Schristosset cannot_resolve_re "Cannot resolve method S::method to any overloaded instance"
2507163879Schristos
2607163879Schristos# Build an "Cannot resolve method ..." expected error string for
2707163879Schristos# method METH.
2807163879Schristos#
2907163879Schristosproc cannot_resolve {meth} {
3007163879Schristos    return "Cannot resolve method $meth to any overloaded instance"
3107163879Schristos}
3207163879Schristos
3307163879Schristos# A list of scopes that have the static variables that we want to
3407163879Schristos# print.  Each entry has, in order, the scope/function name, and the
3507163879Schristos# prefix used by the static variables.  The prefix exists both to make
3607163879Schristos# it easier to debug the test if something goes wrong, and, to make
3707163879Schristos# sure that printing the static local of one method overload doesn't
3807163879Schristos# find the variables of the wrong overload.
3907163879Schristos#
4007163879Schristos# While at it, we also try printing each scope without the static
4107163879Schristos# local, to check that the parse copes with cv overloads without
4207163879Schristos# quoting.  That's what the third and forth columns are for.  Note
4307163879Schristos# that printing "func()" is different from "func(void)".  The former
4407163879Schristos# is an inferior function call, while the latter is a reference to the
4507163879Schristos# function.
4607163879Schristos
4707163879Schristos     #SCOPE				#PREFIX         #PRINT-SCOPE-QUOTED
4807163879Schristos							#PRINT-SCOPE-UNQUOTED (opt)
4907163879Schristosset cxx_scopes_list {
5007163879Schristos    {"S::method()"			"S_M"		{= \\{void \\(S \\* const\\)\\} $hex <S::method\\(\\)>}
5107163879Schristos							{[cannot_resolve "S::method"]}}
5207163879Schristos
5307163879Schristos    {"S::method() const"		"S_M_C"		{= \\{void \\(const S \\* const\\)\\} $hex <S::method\\(\\) const>}
5407163879Schristos							$syntax_re}
5507163879Schristos
5607163879Schristos    {"S::method() volatile"		"S_M_V"		{= \\{void \\(volatile S \\* const\\)\\} $hex <S::method\\(\\) volatile>}
5707163879Schristos							$syntax_re}
5807163879Schristos
5907163879Schristos    {"S::method() const volatile"	"S_M_CV"	{= \\{void \\(const volatile S \\* const\\)\\} $hex <S::method\\(\\) const volatile>}
6007163879Schristos							$syntax_re}
6107163879Schristos
6207163879Schristos    {"S::method() volatile const"	"S_M_CV"	{= \\{void \\(const volatile S \\* const\\)\\} $hex <S::method\\(\\) const volatile>}
6307163879Schristos							$syntax_re}
6407163879Schristos
6507163879Schristos    {"S::method(void)"			"S_M"		{= \\{void \\(S \\* const\\)\\} $hex <S::method\\(\\)>}}
6607163879Schristos    {"S::method(void) const"		"S_M_C"		{= \\{void \\(const S \\* const\\)\\} $hex <S::method\\(\\) const>}}
6707163879Schristos    {"S::method(void) volatile"		"S_M_V"		{= \\{void \\(volatile S \\* const\\)\\} $hex <S::method\\(\\) volatile>}}
6807163879Schristos    {"S::method(void) const volatile"	"S_M_CV"	{= \\{void \\(const volatile S \\* const\\)\\} $hex <S::method\\(\\) const volatile>}}
6907163879Schristos    {"S::method(void) volatile const"	"S_M_CV"	{= \\{void \\(const volatile S \\* const\\)\\} $hex <S::method\\(\\) const volatile>}}
7007163879Schristos
7107163879Schristos    {"S::static_method()"		"S_SM"		{= \\{void \\(void\\)\\} $hex <S::static_method\\(\\)>}
7207163879Schristos							"void"}
7307163879Schristos
7407163879Schristos    {"S::static_method(void)"		"S_SM"		{= \\{void \\(void\\)\\} $hex <S::static_method\\(\\)>}}
7507163879Schristos
7607163879Schristos    {"S::inline_method()"		"S_IM"		{= \\{void \\(S \\* const\\)\\} $hex <S::inline_method\\(\\)>}
7707163879Schristos							{[cannot_resolve "S::inline_method"]}}
7807163879Schristos
7907163879Schristos    {"S::inline_method(void)"		"S_IM"		{= \\{void \\(S \\* const\\)\\} $hex <S::inline_method\\(\\)>}}
8007163879Schristos
8107163879Schristos    {"S::static_inline_method()"	"S_SIM"		{= \\{void \\(void\\)\\} $hex <S::static_inline_method\\(\\)>}
8207163879Schristos							"void"}
8307163879Schristos
8407163879Schristos    {"S::static_inline_method(void)"	"S_SIM"		{= \\{void \\(void\\)\\} $hex <S::static_inline_method\\(\\)>}}
8507163879Schristos
8607163879Schristos    {"S2<int>::method()"		"S2_M"		{= \\{void \\(S2<int> \\* const\\)\\} $hex <S2<int>::method\\(\\)>}
8707163879Schristos							{[cannot_resolve "S2<int>::method"]}}
8807163879Schristos
8907163879Schristos    {"S2<int>::static_method()"		"S2_SM"		{= \\{void \\(void\\)\\} $hex <S2<int>::static_method\\(\\)>}
9007163879Schristos							"void"}
9107163879Schristos
9207163879Schristos    {"S2<int>::inline_method()"		"S2_IM"		{= \\{void \\(S2<int> \\* const\\)\\} $hex <S2<int>::inline_method\\(\\)>}
9307163879Schristos							{[cannot_resolve "S2<int>::inline_method"]}}
9407163879Schristos
9507163879Schristos    {"S2<int>::static_inline_method()"	"S2_SIM"	{= \\{void \\(void\\)\\} $hex <S2<int>::static_inline_method\\(\\)>}
9607163879Schristos							"void"}
9707163879Schristos
9807163879Schristos    {"free_func"			"FF"		{= \\{void \\(void\\)\\} $hex <free_func\\(\\)>}}
9907163879Schristos
10007163879Schristos    {"free_func()"			"FF"		{= \\{void \\(void\\)\\} $hex <free_func\\(\\)>}
10107163879Schristos							"void"}
10207163879Schristos
10307163879Schristos    {"free_func(void)"			"FF"		{= \\{void \\(void\\)\\} $hex <free_func\\(\\)>}}
10407163879Schristos
10507163879Schristos    {"free_inline_func()"		"FIF"		{= \\{void \\(void\\)\\} $hex <free_inline_func\\(\\)>}
10607163879Schristos							"void"}
10707163879Schristos
10807163879Schristos    {"free_inline_func(void)"		"FIF"		{= \\{void \\(void\\)\\} $hex <free_inline_func\\(\\)>}}
10907163879Schristos}
11007163879Schristos
11107163879Schristosset c_scopes_list {
11207163879Schristos    {"free_func"			"FF"		{= \\{void \\(void\\)\\} $hex <free_func>}}
11307163879Schristos    {"free_inline_func"			"FIF"		{= \\{void \\(void\\)\\} $hex <free_inline_func>}}
11407163879Schristos}
11507163879Schristos
11607163879Schristos# A list of all the static varibles defined in each scope.  The first
11707163879Schristos# column is the name of the variable, without the prefix, and the
11807163879Schristos# second column is a regex matching what printing the variable should
11907163879Schristos# output.
12007163879Schristos
12107163879Schristos     #VAR		#PRINT
12207163879Schristosset vars_list {
12307163879Schristos    {"s_var_int"	" = 4"}
12407163879Schristos    {"s_var_float"	" = 3.14.*"}
12507163879Schristos    {"s_var_aggregate"	" = \\{i1 = 1, i2 = 2, i3 = 3\\}"}
12607163879Schristos}
12707163879Schristos
12807163879Schristosproc do_test {lang} {
12907163879Schristos    global c_scopes_list
13007163879Schristos    global cxx_scopes_list
13107163879Schristos    global vars_list
13207163879Schristos    global srcfile testfile
133*1424dfb3Schristos    global gdb_prompt
13407163879Schristos
13507163879Schristos    set options {debug}
13607163879Schristos
13707163879Schristos    if {$lang == "c++"} {
13807163879Schristos	if { [skip_cplus_tests] } {
13907163879Schristos	    return
14007163879Schristos	}
14107163879Schristos	lappend options $lang
14207163879Schristos	set src ${srcfile}c
14307163879Schristos    } else {
14407163879Schristos	set src ${srcfile}
14507163879Schristos    }
14607163879Schristos
14707163879Schristos    if {[prepare_for_testing "failed to prepare" $testfile-$lang \
14807163879Schristos	     [list $src] $options]} {
14907163879Schristos	return -1
15007163879Schristos    }
15107163879Schristos
15207163879Schristos    if ![runto_main] then {
15307163879Schristos	fail "couldn't run to breakpoint"
15407163879Schristos	return
15507163879Schristos    }
15607163879Schristos
15707163879Schristos    gdb_test "show language" " currently [string_to_regexp $lang]\"\\."
15807163879Schristos
15907163879Schristos    if {$lang == "c"} {
16007163879Schristos	set scopes_list $c_scopes_list
16107163879Schristos    } else {
16207163879Schristos	set scopes_list $cxx_scopes_list
16307163879Schristos    }
16407163879Schristos
16507163879Schristos    # Print each scope/function using these syntaxes:
16607163879Schristos    #
16707163879Schristos    #  "(gdb) p 'S::method() const'"    # quoted
16807163879Schristos    #  "(gdb) p S::method() const"      # unquoted
16907163879Schristos    #
17007163879Schristos    foreach scope_line $scopes_list  {
17107163879Schristos	set scope [lindex $scope_line 0]
17207163879Schristos
17307163879Schristos	set print_quoted_re [lindex $scope_line 2]
17407163879Schristos	set print_quoted_re [uplevel 1 "subst -nobackslashes -nocommands \"$print_quoted_re\""]
17507163879Schristos
17607163879Schristos	set print_unquoted_re [lindex $scope_line 3]
17707163879Schristos	set print_unquoted_re [uplevel 1 "subst -nobackslashes -nocommands \"$print_unquoted_re\""]
17807163879Schristos
17907163879Schristos	gdb_test "print '${scope}'" $print_quoted_re
18007163879Schristos
18107163879Schristos	if {$print_unquoted_re != ""} {
18207163879Schristos	    gdb_test "print ${scope}" $print_unquoted_re
18307163879Schristos	} else {
18407163879Schristos	    gdb_test "print ${scope}" $print_quoted_re
18507163879Schristos	}
18607163879Schristos    }
18707163879Schristos
18807163879Schristos    # Print each variable using these syntaxes:
18907163879Schristos    #
19007163879Schristos    #  'func()'::var
19107163879Schristos    #  func()::var
19207163879Schristos    #  'func()::var'
19307163879Schristos    #
19407163879Schristos    # In C++, the latter case makes sure that symbol lookup finds the
19507163879Schristos    # debug symbol instead of the minimal symbol with that exact same
19607163879Schristos    # name.
19707163879Schristos
19807163879Schristos    foreach scope_line $scopes_list  {
19907163879Schristos	set scope [lindex $scope_line 0]
20007163879Schristos	set var_prefix [lindex $scope_line 1]
20107163879Schristos	foreach var_line $vars_list {
20207163879Schristos	    set var [lindex $var_line 0]
20307163879Schristos	    set print_re [lindex $var_line 1]
20407163879Schristos
205*1424dfb3Schristos	    # The gcc PR debug/55541 has the effect that local statics are
206*1424dfb3Schristos	    # wrapped in a DW_TAG_lexical_block, making them unaddressable from
207*1424dfb3Schristos	    # outside the function.  XFAIL the relevant tests.
208*1424dfb3Schristos	    set test "print '${scope}'::${var_prefix}_${var}"
209*1424dfb3Schristos	    set xfail_pattern "No symbol \".*\" in specified context."
210*1424dfb3Schristos	    gdb_test_multiple $test $test {
211*1424dfb3Schristos		-re "\[\r\n\]*(?:$print_re)\[\r\n\]+$gdb_prompt $" {
212*1424dfb3Schristos		    pass $test
213*1424dfb3Schristos		}
214*1424dfb3Schristos		-re "\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" {
215*1424dfb3Schristos		    xfail $test
216*1424dfb3Schristos		}
217*1424dfb3Schristos	    }
218*1424dfb3Schristos	    set test "print ${scope}::${var_prefix}_${var}"
219*1424dfb3Schristos	    gdb_test_multiple $test $test {
220*1424dfb3Schristos		-re "\[\r\n\]*(?:$print_re)\[\r\n\]+$gdb_prompt $" {
221*1424dfb3Schristos		    pass $test
222*1424dfb3Schristos		}
223*1424dfb3Schristos		-re "\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" {
224*1424dfb3Schristos		    xfail $test
225*1424dfb3Schristos		}
226*1424dfb3Schristos	    }
22707163879Schristos
22807163879Schristos	    set sym "${scope}::${var_prefix}_${var}"
22907163879Schristos	    if {$lang == "c++"} {
230*1424dfb3Schristos		set test "print '${sym}'"
231*1424dfb3Schristos		set xfail_pattern "No symbol .* in current context."
232*1424dfb3Schristos		set xfail_pattern2 "has unknown type; cast it to its declared type"
233*1424dfb3Schristos		gdb_test_multiple $test $test {
234*1424dfb3Schristos		    -re "\[\r\n\]*(?:$print_re)\[\r\n\]+$gdb_prompt $" {
235*1424dfb3Schristos			pass $test
236*1424dfb3Schristos		    }
237*1424dfb3Schristos		    -re "\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" {
238*1424dfb3Schristos			xfail $test
239*1424dfb3Schristos		    }
240*1424dfb3Schristos		    -re "\[\r\n\]*(?:$xfail_pattern2)\[\r\n\]+$gdb_prompt $" {
241*1424dfb3Schristos			xfail $test
242*1424dfb3Schristos		    }
243*1424dfb3Schristos		}
24407163879Schristos	    } else {
24507163879Schristos		gdb_test "print '${sym}'" "No symbol \"$sym\" in current context\\."
24607163879Schristos	    }
24707163879Schristos	}
24807163879Schristos    }
24907163879Schristos
25007163879Schristos    # Now run to each function, and print its variables using the
25107163879Schristos    # localy-visible name.
25207163879Schristos    foreach scope_line $scopes_list {
25307163879Schristos	set scope [lindex $scope_line 0]
25407163879Schristos	set var_prefix [lindex $scope_line 1]
25507163879Schristos
25607163879Schristos	with_test_prefix "$scope" {
25707163879Schristos	    delete_breakpoints
25807163879Schristos	    gdb_breakpoint "$scope"
25907163879Schristos	    gdb_continue_to_breakpoint "$scope"
26007163879Schristos
26107163879Schristos	    foreach var_line $vars_list {
26207163879Schristos		set var [lindex $var_line 0]
26307163879Schristos		set print_re [lindex $var_line 1]
26407163879Schristos
26507163879Schristos		gdb_test "print ${var_prefix}_${var}" $print_re
26607163879Schristos	    }
26707163879Schristos	}
26807163879Schristos    }
26907163879Schristos}
27007163879Schristos
27107163879Schristosforeach lang {"c" "c++"} {
27207163879Schristos    with_test_prefix $lang {
27307163879Schristos	do_test $lang
27407163879Schristos    }
27507163879Schristos}
276