1# Copyright 2002-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# Tests for member static data
17# 2002-05-13  Benjamin Kosnik  <bkoz@redhat.com>
18# 2002-08-22  David Carlton <carlton@math.stanford.edu>
19
20# This file is part of the gdb testsuite
21
22if { [skip_cplus_tests] } { return }
23
24#
25# test running programs
26#
27
28standard_testfile .cc m-static1.cc
29
30set flags [list debug c++]
31if { [test_compiler_info gcc-*] && [gcc_major_version] >= 10 } {
32    # Work around PR gcc/101452.
33    lappend flags additional_flags=-fno-eliminate-unused-debug-types
34}
35
36if {[prepare_for_testing "failed to prepare" $testfile \
37	 [list $srcfile $srcfile2] $flags]} {
38    return -1
39}
40
41if {![runto_main]} {
42    return
43}
44
45get_debug_format
46set non_dwarf [expr ! [test_debug_format "DWARF \[0-9\]"]]
47
48# First, run to after we've constructed all the objects:
49
50gdb_breakpoint [gdb_get_line_number "constructs-done"]
51gdb_continue_to_breakpoint "end of constructors"
52
53
54# One.
55
56# simple object, static const bool
57gdb_test "print test1.test" "\\$\[0-9\]* = true" "simple object, static const bool"
58
59# simple object, static const int
60gdb_test "print test1.key1" "\\$\[0-9\]* = 5" "simple object, static const int"
61
62# simple object, static long
63gdb_test "print test1.key2" "\\$\[0-9\]* = 77" "simple object, static long"
64
65# simple object, static enum
66gdb_test "print test1.value" "\\$\[0-9\]* = oriental" "simple object, static enum"
67
68if { [is_aarch32_target] } {
69    gdb_test "print test5.single_constructor" \
70	{ = {single_constructor \*\(single_constructor \* const\)} 0x[0-9a-f]+ <single_constructor::single_constructor\(\)>} \
71	"simple object instance, print constructor"
72    gdb_test "ptype test5.single_constructor" \
73	{type = class single_constructor {\r\n  public:\r\n    single_constructor\(void\);\r\n    ~single_constructor\(\);\r\n} \*\(single_constructor \* const\)} \
74	"simple object instance, ptype constructor"
75    gdb_test "ptype single_constructor::single_constructor" \
76	{type = class single_constructor {\r\n  public:\r\n    single_constructor\(void\);\r\n    ~single_constructor\(\);\r\n} \*\(single_constructor \* const\)} \
77	"simple object class, ptype constructor"
78
79    gdb_test "print test1.~gnu_obj_1" \
80	{ = {void \*\(gnu_obj_1 \* const, int\)} 0x[0-9a-f]+ <gnu_obj_1::~gnu_obj_1\(\)>} \
81	"simple object instance, print destructor"
82    gdb_test "ptype test1.~gnu_obj_1" \
83	{type = void \*\(gnu_obj_1 \* const, int\)} \
84	"simple object instance, ptype destructor"
85
86    gdb_test "print test1.'~gnu_obj_1'" \
87	{ = {void \*\(gnu_obj_1 \*( const)?, int\)} 0x[0-9a-f]+ <gnu_obj_1::~gnu_obj_1\(\)>} \
88	"simple object instance, print quoted destructor"
89
90    gdb_test "ptype gnu_obj_1::'~gnu_obj_1'" \
91	{type = void \*\(gnu_obj_1 \* const\)} \
92	"simple object class, ptype quoted destructor"
93} else {
94    gdb_test "print test5.single_constructor" \
95	{ = {void \(single_constructor \* const\)} 0x[0-9a-f]+ <single_constructor::single_constructor\(\)>} \
96	"simple object instance, print constructor"
97    gdb_test "ptype test5.single_constructor" \
98	{type = void \(single_constructor \* const\)} \
99	"simple object instance, ptype constructor"
100    gdb_test "ptype single_constructor::single_constructor" \
101	{type = void \(single_constructor \* const\)} \
102	"simple object class, ptype constructor"
103
104    gdb_test "print test1.~gnu_obj_1"\
105	{ = {void \(gnu_obj_1 \* const(?:, int)?\)} 0x[0-9a-f]+ <gnu_obj_1::~gnu_obj_1\(\)>}\
106	"simple object instance, print destructor"
107
108    gdb_test "ptype test1.~gnu_obj_1"\
109	{type = void \(gnu_obj_1 \* const(?:, int)?\)}\
110	"simple object instance, ptype destructor"
111
112    gdb_test "print test1.'~gnu_obj_1'" \
113	{ = {void \(gnu_obj_1 \*( const)?(?:, int)?\)} 0x[0-9a-f]+ <gnu_obj_1::~gnu_obj_1\(\)>} \
114	"simple object instance, print quoted destructor"
115
116    gdb_test "ptype gnu_obj_1::'~gnu_obj_1'" \
117	{type = void \(gnu_obj_1 \* const\)} \
118	"simple object class, ptype quoted destructor"
119}
120
121# Two.
122
123# derived template object, base static const bool
124gdb_test "print test2.test" "\\$\[0-9\]* = true" "derived template object, base static const bool"
125
126# derived template object, base static const int
127gdb_test "print test2.key1" "\\$\[0-9\]* = 5" "derived template object, base static const int"
128
129# derived template object, base static long
130gdb_test "print test2.key2" "\\$\[0-9\]* = 77" "derived template object, base static long"
131
132# derived template object, base static enum
133gdb_test "print test2.value" "\\$\[0-9\].* = oriental" "derived template object, base static enum"
134
135# derived template object, static enum
136gdb_test "print test2.value_derived" "\\$\[0-9\].* = etruscan" "derived template object, static enum"
137
138# Three.
139
140# template object, static derived template data member's base static const bool
141gdb_test "print test3.data.test" "\\$\[0-9\].* = true" "template object, static const bool"
142
143# template object, static derived template data member's base static const int
144gdb_test "print test3.data.key1" "\\$\[0-9\].* = 5" "template object, static const int"
145
146# template object, static derived template data member's base static long
147gdb_test "print test3.data.key2" "\\$\[0-9\].* = 77" "template object, static long"
148
149# template object, static derived template data member's base static enum
150gdb_test "print test3.data.value" "\\$\[0-9\].* = oriental" "template object, static enum"
151
152#  template object, static derived template data member's static enum
153gdb_test "print test3.data.value_derived" "\\$\[0-9\].* = etruscan" "template object, static derived enum"
154
155# 2002-08-16
156# Four.
157
158# static const int initialized in another file.
159gdb_test "print test4.elsewhere" "\\$\[0-9\].* = 221" "static const int initialized elsewhere"
160
161# static const int that nobody initializes.  From PR gdb/635.
162if {[test_compiler_info {gcc-[0-3]-*}]
163    || [test_compiler_info {gcc-4-[0-4]-*}]} {
164    # There was an extra CU-level DW_TAG_variable as DW_AT_declaration
165    # with DW_AT_name = nowhere
166    # and DW_AT_MIPS_linkage_name = _ZN9gnu_obj_47nowhereE .
167    setup_xfail *-*-*
168}
169gdb_test "print test4.nowhere" "<optimized out>" "static const int initialized nowhere (print field)"
170gdb_test "ptype test4.nowhere" "type = const int"
171gdb_test "print test4.nowhere.nowhere" "Attempt to extract a component of a value that is not a structure."
172
173# Same, but print the whole struct.
174gdb_test "print test4" "static nowhere = <optimized out>.*" "static const int initialized nowhere (whole struct)"
175
176# static const initialized in the class definition, PR gdb/11702.
177if { $non_dwarf } { setup_xfail *-*-* }
178gdb_test "print test4.everywhere" "\\$\[0-9\].* = 317" "static const int initialized in class definition"
179if { $non_dwarf } { setup_xfail *-*-* }
180gdb_test "print test4.somewhere" "\\$\[0-9\].* = 3.14\[0-9\]*" "static const float initialized in class definition"
181
182# Also make sure static const members can be found via "info var".
183if { $non_dwarf } { setup_xfail *-*-* }
184gdb_test "info variable everywhere" "File .*/m-static\[.\]h.*const int gnu_obj_4::everywhere;"
185
186# Perhaps at some point test4 should also include a test for a static
187# const int that was initialized in the header file.  But I'm not sure
188# that GDB's current behavior in such situations is either consistent
189# across platforms or optimal, so I'm not including one now.
190
191# Step into test1.method and examine the method-scoped static.
192# This is a regression test for PR 9708.
193gdb_test "step" "gnu_obj_1::method.*"
194gdb_test "print svar" " = true"
195
196gdb_exit
197return 0
198