1 # Copyright 2017-2019 Free Software Foundation, Inc.
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.
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.
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/>.
16 # Tests for function local static variables, both C and C++.
18 # This file is part of the gdb testsuite.
22 # A few expected errors.
23 set syntax_re "A syntax error in expression, near.*"
24 set cannot_resolve_re "Cannot resolve method S::method to any overloaded instance"
26 # Build an "Cannot resolve method ..." expected error string for
29 proc cannot_resolve {meth} {
30 return "Cannot resolve method $meth to any overloaded instance"
33 # A list of scopes that have the static variables that we want to
34 # print. Each entry has, in order, the scope/function name, and the
35 # prefix used by the static variables. The prefix exists both to make
36 # it easier to debug the test if something goes wrong, and, to make
37 # sure that printing the static local of one method overload doesn't
38 # find the variables of the wrong overload.
40 # While at it, we also try printing each scope without the static
41 # local, to check that the parse copes with cv overloads without
42 # quoting. That's what the third and forth columns are for. Note
43 # that printing "func()" is different from "func(void)". The former
44 # is an inferior function call, while the latter is a reference to the
47 #SCOPE #PREFIX #PRINT-SCOPE-QUOTED
48 #PRINT-SCOPE-UNQUOTED (opt)
50 {"S::method()" "S_M" {= \\{void \\(S \\* const\\)\\} $hex <S::method\\(\\)>}
51 {[cannot_resolve "S::method"]}}
53 {"S::method() const" "S_M_C" {= \\{void \\(const S \\* const\\)\\} $hex <S::method\\(\\) const>}
56 {"S::method() volatile" "S_M_V" {= \\{void \\(volatile S \\* const\\)\\} $hex <S::method\\(\\) volatile>}
59 {"S::method() const volatile" "S_M_CV" {= \\{void \\(const volatile S \\* const\\)\\} $hex <S::method\\(\\) const volatile>}
62 {"S::method() volatile const" "S_M_CV" {= \\{void \\(const volatile S \\* const\\)\\} $hex <S::method\\(\\) const volatile>}
65 {"S::method(void)" "S_M" {= \\{void \\(S \\* const\\)\\} $hex <S::method\\(\\)>}}
66 {"S::method(void) const" "S_M_C" {= \\{void \\(const S \\* const\\)\\} $hex <S::method\\(\\) const>}}
67 {"S::method(void) volatile" "S_M_V" {= \\{void \\(volatile S \\* const\\)\\} $hex <S::method\\(\\) volatile>}}
68 {"S::method(void) const volatile" "S_M_CV" {= \\{void \\(const volatile S \\* const\\)\\} $hex <S::method\\(\\) const volatile>}}
69 {"S::method(void) volatile const" "S_M_CV" {= \\{void \\(const volatile S \\* const\\)\\} $hex <S::method\\(\\) const volatile>}}
71 {"S::static_method()" "S_SM" {= \\{void \\(void\\)\\} $hex <S::static_method\\(\\)>}
74 {"S::static_method(void)" "S_SM" {= \\{void \\(void\\)\\} $hex <S::static_method\\(\\)>}}
76 {"S::inline_method()" "S_IM" {= \\{void \\(S \\* const\\)\\} $hex <S::inline_method\\(\\)>}
77 {[cannot_resolve "S::inline_method"]}}
79 {"S::inline_method(void)" "S_IM" {= \\{void \\(S \\* const\\)\\} $hex <S::inline_method\\(\\)>}}
81 {"S::static_inline_method()" "S_SIM" {= \\{void \\(void\\)\\} $hex <S::static_inline_method\\(\\)>}
84 {"S::static_inline_method(void)" "S_SIM" {= \\{void \\(void\\)\\} $hex <S::static_inline_method\\(\\)>}}
86 {"S2<int>::method()" "S2_M" {= \\{void \\(S2<int> \\* const\\)\\} $hex <S2<int>::method\\(\\)>}
87 {[cannot_resolve "S2<int>::method"]}}
89 {"S2<int>::static_method()" "S2_SM" {= \\{void \\(void\\)\\} $hex <S2<int>::static_method\\(\\)>}
92 {"S2<int>::inline_method()" "S2_IM" {= \\{void \\(S2<int> \\* const\\)\\} $hex <S2<int>::inline_method\\(\\)>}
93 {[cannot_resolve "S2<int>::inline_method"]}}
95 {"S2<int>::static_inline_method()" "S2_SIM" {= \\{void \\(void\\)\\} $hex <S2<int>::static_inline_method\\(\\)>}
98 {"free_func" "FF" {= \\{void \\(void\\)\\} $hex <free_func\\(\\)>}}
100 {"free_func()" "FF" {= \\{void \\(void\\)\\} $hex <free_func\\(\\)>}
103 {"free_func(void)" "FF" {= \\{void \\(void\\)\\} $hex <free_func\\(\\)>}}
105 {"free_inline_func()" "FIF" {= \\{void \\(void\\)\\} $hex <free_inline_func\\(\\)>}
108 {"free_inline_func(void)" "FIF" {= \\{void \\(void\\)\\} $hex <free_inline_func\\(\\)>}}
112 {"free_func" "FF" {= \\{void \\(void\\)\\} $hex <free_func>}}
113 {"free_inline_func" "FIF" {= \\{void \\(void\\)\\} $hex <free_inline_func>}}
116 # A list of all the static varibles defined in each scope. The first
117 # column is the name of the variable, without the prefix, and the
118 # second column is a regex matching what printing the variable should
124 {"s_var_float" " = 3.14.*"}
125 {"s_var_aggregate" " = \\{i1 = 1, i2 = 2, i3 = 3\\}"}
128 proc do_test {lang} {
130 global cxx_scopes_list
132 global srcfile testfile
136 if {$lang == "c++"} {
137 if { [skip_cplus_tests] } {
140 lappend options $lang
146 if {[prepare_for_testing "failed to prepare" $testfile-$lang \
147 [list $src] $options]} {
151 if ![runto_main] then {
152 fail "couldn't run to breakpoint"
156 gdb_test "show language" " currently [string_to_regexp $lang]\"\\."
159 set scopes_list $c_scopes_list
161 set scopes_list $cxx_scopes_list
164 # Print each scope/function using these syntaxes:
166 # "(gdb) p 'S::method() const'" # quoted
167 # "(gdb) p S::method() const" # unquoted
169 foreach scope_line $scopes_list {
170 set scope [lindex $scope_line 0]
172 set print_quoted_re [lindex $scope_line 2]
173 set print_quoted_re [uplevel 1 "subst -nobackslashes -nocommands \"$print_quoted_re\""]
175 set print_unquoted_re [lindex $scope_line 3]
176 set print_unquoted_re [uplevel 1 "subst -nobackslashes -nocommands \"$print_unquoted_re\""]
178 gdb_test "print '${scope}'" $print_quoted_re
180 if {$print_unquoted_re != ""} {
181 gdb_test "print ${scope}" $print_unquoted_re
183 gdb_test "print ${scope}" $print_quoted_re
187 # Print each variable using these syntaxes:
193 # In C++, the latter case makes sure that symbol lookup finds the
194 # debug symbol instead of the minimal symbol with that exact same
197 foreach scope_line $scopes_list {
198 set scope [lindex $scope_line 0]
199 set var_prefix [lindex $scope_line 1]
200 foreach var_line $vars_list {
201 set var [lindex $var_line 0]
202 set print_re [lindex $var_line 1]
204 gdb_test "print '${scope}'::${var_prefix}_${var}" $print_re
205 gdb_test "print ${scope}::${var_prefix}_${var}" $print_re
207 set sym "${scope}::${var_prefix}_${var}"
208 if {$lang == "c++"} {
209 gdb_test "print '${sym}'" $print_re
211 gdb_test "print '${sym}'" "No symbol \"$sym\" in current context\\."
216 # Now run to each function, and print its variables using the
217 # localy-visible name.
218 foreach scope_line $scopes_list {
219 set scope [lindex $scope_line 0]
220 set var_prefix [lindex $scope_line 1]
222 with_test_prefix "$scope" {
224 gdb_breakpoint "$scope"
225 gdb_continue_to_breakpoint "$scope"
227 foreach var_line $vars_list {
228 set var [lindex $var_line 0]
229 set print_re [lindex $var_line 1]
231 gdb_test "print ${var_prefix}_${var}" $print_re
237 foreach lang {"c" "c++"} {
238 with_test_prefix $lang {