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