1# Copyright 2018-2023 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# Make sure printing virtual base class data member works correctly (PR16841)
17
18if { [skip_cplus_tests] } { return }
19
20standard_testfile .cc
21
22if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
23    return -1
24}
25
26if {![runto_main]} {
27    return
28}
29
30# From a list of nested scopes, generate all possible ways of accessing something
31# in those scopes.  For example, with the argument {foo bar baz}, this proc will
32# return:
33#  - {} (empty string)
34#  - baz::
35#  - bar::
36#  - bar::baz::
37#  - foo::
38#  - foo::baz::
39#  - foo::bar::
40#  - foo::bar::baz::
41
42proc make_scope_list { scopes } {
43    if { [llength $scopes] == 1 } {
44        return [list "" "${scopes}::"]
45    }
46
47    # Pop the first element, save the first scope.
48    set this_scope [lindex $scopes 0]
49    set scopes [lreplace $scopes 0 0]
50
51    set child_result [make_scope_list $scopes]
52
53    # Add a copy of the child's result without this scope...
54    set result $child_result
55
56    # ... and a copy of the child's result with this scope.
57    foreach r $child_result {
58        lappend result "${this_scope}::$r"
59    }
60
61    return $result
62}
63
64proc test_variables_in_base { scopes } {
65  with_test_prefix "$scopes" {
66      foreach scope [make_scope_list $scopes] {
67	  gdb_test "print ${scope}i" " = 55"
68	  gdb_test "print ${scope}d" " = 6.25"
69	  gdb_test "print ${scope}x" " = 22"
70      }
71  }
72}
73
74proc test_variables_in_superbase { scopes } {
75  with_test_prefix "$scopes" {
76      foreach scope [make_scope_list $scopes] {
77	  gdb_test "print ${scope}x" " = 22"
78      }
79  }
80}
81
82proc test_variables_in_super { scopes } {
83  with_test_prefix "$scopes" {
84      foreach scope [make_scope_list $scopes] {
85	  gdb_test "print ${scope}w" " = 17"
86      }
87  }
88}
89
90with_test_prefix "derived::func_d" {
91    gdb_breakpoint "derived::func_d"
92    gdb_continue_to_breakpoint "continue to derived::func_d"
93    test_variables_in_base {derived base}
94    test_variables_in_superbase {derived base superbase}
95    test_variables_in_superbase {base superbase}
96    test_variables_in_superbase {derived superbase}
97    test_variables_in_superbase {superbase}
98    test_variables_in_superbase {base}
99    test_variables_in_super {super}
100    test_variables_in_super {derived super}
101}
102
103with_test_prefix "foo::func_f" {
104    gdb_breakpoint "foo::func_f"
105    gdb_continue_to_breakpoint "continue to foo::func_f"
106    test_variables_in_base {foo derived base}
107    test_variables_in_base {foo base}
108    test_variables_in_base {base}
109    test_variables_in_superbase {superbase}
110    test_variables_in_superbase {foo superbase}
111    test_variables_in_superbase {foo derived superbase}
112    test_variables_in_superbase {foo derived base superbase}
113    test_variables_in_super {super}
114    test_variables_in_super {foo super}
115    test_variables_in_super {foo derived super}
116}
117