]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.cp/namespace.exp
gdb/cp-namespace.c: Fix assert failure caused by malformed user input
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.cp / namespace.exp
1 # Copyright 1997-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 namespaces
17 # Originally written by Satish Pai <pai@apollo.hp.com> 1997-07-23
18
19 # This file is part of the gdb testsuite
20
21 # Note: The original tests were geared to the HP aCC compiler,
22 # which has an idiosyncratic way of emitting debug info
23 # for namespaces.
24 # Note: As of 2000-06-03, they passed under g++ - djb
25
26
27
28 require allow_cplus_tests
29
30 standard_testfile .cc namespace1.cc
31
32 set xfail_class_types 0
33 if {[test_compiler_info {gcc-[0-3]-*}]
34 || [test_compiler_info {gcc-4-[0-4]-*}]} {
35 # The type in class is missing in older GCCs.
36 set xfail_class_types 1
37 }
38
39 if {[prepare_for_testing "failed to prepare" $testfile \
40 [list $srcfile $srcfile2] {debug c++}]} {
41 return -1
42 }
43
44 gdb_test "show lang" "auto; currently c\\+\\+.*"
45
46 #
47 # set it up at a breakpoint so we can play with the variable values
48 #
49 if {![runto_main]} {
50 return
51 }
52
53 if {![runto 'marker1']} {
54 return
55 }
56
57 gdb_test "up" ".*main.*" "up from marker1"
58
59 # Access a data item inside a namespace using colons and
60 # single quotes. :-(
61
62 # NOTE: carlton/2003-09-24: the quotes are becoming less necessary (or
63 # even desirable.) For tests where it should still work with quotes,
64 # I'm including versions both with and without quotes; for tests that
65 # shouldn't work with quotes, I'm only including one version.
66
67 gdb_test "print 'AAA::c'" \
68 "\\$\[0-9\]* = 0 '\\\\(0|000)'"
69
70 gdb_test "print AAA::c" \
71 "\\$\[0-9\]* = 0 '\\\\(0|000)'"
72
73 # An object declared using "using".
74
75 gdb_test "print ina" "\\$\[0-9\]+ = {xx = 33}"
76
77 gdb_test_multiple "ptype ina" "ptype ina" {
78 -re "type = class (AAA::|)inA \{\r\n\[ \]*public:\r\n\[ \]*int xx;\r\n\[ \]*\r\n\[ \]*.*int fum\\(int\\);\r\n\}\r\n$gdb_prompt $" {
79 pass "ptype ina"
80 }
81 -re "type = class (AAA::|)inA \{\r\n\[ \]*public:\r\n\[ \]*int xx;\r\n\[ \]*\r\n\[ \]*int fum\\(int\\);\r\n.*\}\r\n$gdb_prompt $" {
82 pass "ptype ina"
83 }
84 }
85
86 # Check all functions are known to GDB
87
88 setup_xfail hppa*-*-*11* CLLbs14869
89 gdb_test_multiple "info func xyzq" "info func xyzq" {
90 -re "All functions.*File.*namespace.cc:\r\n.*\tint AAA::A_xyzq\\(int\\);\r\n.*\tint BBB::B_xyzq\\(int\\);\r\n.*\tchar AAA::xyzq\\(char\\);\r\n.*\tchar BBB::xyzq\\(char\\);\r\n.*\tchar BBB::CCC::xyzq\\(char\\);\r\n.*\tchar BBB::Class::xyzq\\(char\\);\r\n$gdb_prompt $" {
91 pass "info func xyzq"
92 }
93 -re "All functions.*File.*namespace.cc:\r\n.*\tint AAA::A_xyzq\\(int\\);\r\n.*\tchar AAA::xyzq\\(char\\);\r\n.*\tint BBB::B_xyzq\\(int\\);\r\n.*\tchar BBB::CCC::xyzq\\(char\\);\r\n.*\tchar BBB::Class::xyzq\\(char\\);\r\n.*\tchar BBB::xyzq\\(char\\);\r\n$gdb_prompt $" {
94 pass "info func xyzq"
95 }
96 }
97
98 # Call a function in a namespace
99
100 gdb_test "print 'AAA::xyzq'('x')" \
101 "\\$\[0-9\]* = 97 'a'"
102
103 gdb_test "print AAA::xyzq('x')" \
104 "\\$\[0-9\]* = 97 'a'"
105
106 # Break on a function in a namespace
107
108 gdb_test "break AAA::xyzq" \
109 "Breakpoint.*at $hex: file.*namespace.cc, line 47\\."
110
111 # Break on a function in the global namespace.
112
113 gdb_test "break -qualified ::ensureOtherRefs" \
114 "Breakpoint.*at $hex: file.*$srcfile2, line $decimal\\."
115
116 # Call a function in a nested namespace
117
118 gdb_test "print 'BBB::CCC::xyzq'('x')" \
119 "\\$\[0-9\]* = 122 'z'"
120
121 gdb_test "print BBB::CCC::xyzq('x')" \
122 "\\$\[0-9\]* = 122 'z'"
123
124 # Break on a function in a nested namespace
125
126 gdb_test "break BBB::CCC::xyzq" \
127 "Breakpoint.*at $hex: file.*namespace.cc, line 63\\."
128
129 # Break on the same function, starting with the global namespace.
130
131 gdb_test "break ::BBB::CCC::xyzq" \
132 ".*Breakpoint.*at $hex: file.*$srcfile, line 63\\."
133
134 # Print address of a function in a class in a namespace
135
136 gdb_test "print 'BBB::Class::xyzq'" \
137 "\\$\[0-9\]* = \{char \\((BBB::|)Class \\*( const|), (char|int)\\)\} $hex <BBB::Class::xyzq\\(char\\)>"
138
139 gdb_test "print BBB::Class::xyzq" \
140 "\\$\[0-9\]* = \{char \\((BBB::|)Class \\*( const|), (char|int)\\)\} $hex <BBB::Class::xyzq\\(char\\)>"
141
142 # Break on a function in a class in a namespace
143
144 gdb_test "break BBB::Class::xyzq" \
145 "Breakpoint.*at $hex: file.*namespace.cc, line 68\\."
146
147 # Tests accessing static elements in namespace of other file.
148
149 gdb_test "whatis C::cOtherFileType" "type = short"
150 gdb_test "whatis ::C::cOtherFileType" "type = short"
151 gdb_test "whatis C::cOtherFileVar" "type = const C::cOtherFileType"
152 gdb_test "whatis ::C::cOtherFileVar" "type = const C::cOtherFileType"
153 gdb_test "print C::cOtherFileVar" "\\$\[0-9\].* = 319"
154 gdb_test "print ::C::cOtherFileVar" "\\$\[0-9\].* = 319"
155
156 if $xfail_class_types { setup_xfail *-*-* }
157 gdb_test "whatis C::OtherFileClass::cOtherFileClassType" "type = short"
158 if $xfail_class_types { setup_xfail *-*-* }
159 gdb_test "whatis ::C::OtherFileClass::cOtherFileClassType" "type = short"
160 gdb_test "print C::OtherFileClass::cOtherFileClassVar" " = 318"
161
162 # FSF GCC <=4.4 creates unqualified DIE "cOtherFileClassVar" ignoring the
163 # namespace the same way older GDB did.
164 set test "print ::cOtherFileClassVar"
165 set test2 "print ::C::OtherFileClass::cOtherFileClassVar"
166 gdb_test_multiple $test $test {
167 -re "No symbol \"cOtherFileClassVar\" in current context\\.\r\n$gdb_prompt $" {
168 pass $test
169
170 gdb_test $test2 " = 318"
171 }
172 -re "\\$\[0-9\].* = 318\r\n$gdb_prompt $" {
173 # Do not permit to XFAIL on recent GCCs.
174 if $xfail_class_types {
175 setup_xfail *-*-*
176 fail $test
177 # Unresolved means human intervention is required to determine
178 # whether the test passed or failed. Since the previous test
179 # xfailed (not failed) human intervention isn't going to help here.
180 # Thus test2 is marked as unsupported instead of unresolved.
181 unsupported $test2
182 } else {
183 fail $test
184 unresolved $test2
185 }
186 }
187 }
188
189 # Test to see if the appropriate namespaces are in scope when trying
190 # to print out stuff from within a function defined within a
191 # namespace.
192
193 if {![runto "C::D::marker2"]} {
194 return
195 }
196
197 gdb_test "print c" "\\$\[0-9\].* = 1"
198 gdb_test "print cc" "No symbol \"cc\" in current context."
199 gdb_test "print 'C::cc'" "\\$\[0-9\].* = 2"
200 gdb_test "print C::cc" "\\$\[0-9\].* = 2"
201 gdb_test "print cd" "\\$\[0-9\].* = 3"
202 gdb_test "print C::D::cd" "No type \"D\" within class or namespace \"C::C\"."
203 gdb_test "print 'E::cde'" "\\$\[0-9\].* = 5"
204 gdb_test "print E::cde" "\\$\[0-9\].* = 5"
205 gdb_test "print shadow" "\\$\[0-9\].* = 13"
206 gdb_test "print E::ce" "No symbol \"ce\" in namespace \"C::D::E\"."
207 gdb_test "ptype C" "type = namespace C::C"
208 gdb_test "ptype E" "type = namespace C::D::E"
209
210 gdb_test "ptype CClass" "type = (class C::CClass \{\r\n public:|struct C::CClass \{)\r\n int x;\r\n\}"
211 gdb_test "ptype CClass::NestedClass" "type = (class C::CClass::NestedClass \{\r\n public:|struct C::CClass::NestedClass \{)\r\n int y;\r\n\}"
212 gdb_test "ptype NestedClass" "No symbol \"NestedClass\" in current context."
213 gdb_test "ptype ::C::CClass" "type = class C::CClass \{\r\n public:\r\n int x;\r\n\}"
214 gdb_test "ptype ::C::CClass::NestedClass" "type = class C::CClass::NestedClass \{\r\n public:\r\n int y;\r\n\}"
215 gdb_test "ptype ::C::NestedClass" "No symbol \"NestedClass\" in namespace \"C\"."
216 gdb_test "ptype C::CClass" "No symbol \"CClass\" in namespace \"C::C\"."
217 gdb_test "ptype C::CClass::NestedClass" "No type \"CClass\" within class or namespace \"C::C\"."
218 gdb_test "ptype C::NestedClass" "No symbol \"NestedClass\" in namespace \"C::C\"."
219
220 # Tests involving multiple files
221
222 gdb_test "print cOtherFile" "\\$\[0-9\].* = 316"
223 gdb_test "ptype OtherFileClass" "type = (class C::OtherFileClass \{\r\n public:|struct C::OtherFileClass \{)\r\n int z;\r\n.*\}"
224 gdb_test "ptype ::C::OtherFileClass" "type = class C::OtherFileClass \{\r\n public:\r\n int z;\r\n.*\}"
225 gdb_test "ptype C::OtherFileClass" "No symbol \"OtherFileClass\" in namespace \"C::C\"."
226
227 # Test class typedefs printing.
228 set expect "type = class C::OtherFileClass \{\r\n.*\r\n *typedef short cOtherFileClassType;\r\n *typedef long cOtherFileClassType2;\r\n\}"
229 if $xfail_class_types { setup_xfail *-*-* }
230 gdb_test "ptype OtherFileClass" $expect "ptype OtherFileClass typedefs"
231 if $xfail_class_types { setup_xfail *-*-* }
232 gdb_test "ptype ::C::OtherFileClass" $expect "ptype ::C::OtherFileClass typedefs"
233
234 # Some anonymous namespace tests.
235
236 gdb_test "print cX" "\\$\[0-9\].* = 6"
237 gdb_test "print 'F::cXf'" "\\$\[0-9\].* = 7"
238 gdb_test "print F::cXf" "\\$\[0-9\].* = 7"
239 gdb_test "print F::cXfX" "\\$\[0-9\].* = 8"
240 gdb_test "print X" "\\$\[0-9\].* = 9"
241 gdb_test "print 'G::Xg'" "\\$\[0-9\].* = 10"
242 gdb_test "print G::Xg" "\\$\[0-9\].* = 10"
243 gdb_test "print G::XgX" "\\$\[0-9\].* = 11"
244 gdb_test "print cXOtherFile" "No symbol \"cXOtherFile\" in current context."
245 gdb_test "print XOtherFile" "No symbol \"XOtherFile\" in current context."
246
247 # Enum tests.
248 gdb_test "print AAA::ALPHA" "\\$\[0-9\].* = AAA::ALPHA"
249
250 # Regression tests for PR 9496.
251 gdb_test "whatis ::C::CClass::NestedClass" "type = C::CClass::NestedClass"
252 gdb_test "whatis ::C::CClass::NestedClass *" "type = C::CClass::NestedClass \\*"
253
254 # Break on functions with a malformed name.
255 gdb_test "break DNE>::DNE" "" "br malformed \'>\'" \
256 "Make breakpoint pending on future shared library load?.*" "y"
257 gdb_test "break DNE)::DNE" "" "br malformed \')\'" \
258 "Make breakpoint pending on future shared library load?.*" "y"