82018c6441b141f4b96a945ea4584bf300159709
[platform/upstream/binutils.git] / gdb / testsuite / gdb.cp / namespace.exp
1 # Copyright 1997-1998, 2000-2004, 2007-2012 Free Software Foundation,
2 # Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 # tests for namespaces
18 # Originally written by Satish Pai <pai@apollo.hp.com> 1997-07-23
19
20 # This file is part of the gdb testsuite
21
22 # Note: The original tests were geared to the HP aCC compiler,
23 # which has an idiosyncratic way of emitting debug info
24 # for namespaces.
25 # Note: As of 2000-06-03, they passed under g++ - djb
26
27
28
29 if { [skip_cplus_tests] } { continue }
30
31 set testfile "namespace"
32 set srcfile ${testfile}.cc
33 set objfile ${objdir}/${subdir}/${testfile}.o
34 set srcfile1 ${testfile}1.cc
35 set objfile1 ${objdir}/${subdir}/${testfile}1.o
36 set binfile ${objdir}/${subdir}/${testfile}
37
38 if [get_compiler_info ${binfile} c++] {
39     return -1;
40 }
41
42 set xfail_class_types 0
43 if {[test_compiler_info {gcc-[0-3]-*}]
44     || [test_compiler_info {gcc-4-[0-4]-*}]} {
45     # The type in class is missing in older GCCs.
46     set xfail_class_types 1
47 }
48
49 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${objfile}" object {debug c++}] != "" } {
50      untested namespace.exp
51      return -1
52 }
53
54 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile1}" "${objfile1}" object {debug c++}] != "" } {
55      untested namespace.exp
56      return -1
57 }
58
59 if  { [gdb_compile "${objfile} ${objfile1}" "${binfile}" executable {debug c++}] != "" } {
60      untested namespace.exp
61      return -1
62 }
63
64 gdb_exit
65 gdb_start
66 gdb_reinitialize_dir $srcdir/$subdir
67 gdb_load ${binfile}
68
69 gdb_test "show lang" "auto; currently c\\+\\+.*"
70
71 #
72 # set it up at a breakpoint so we can play with the variable values
73 #
74 if ![runto_main] then {
75     perror "couldn't run to breakpoint"
76     continue
77 }
78
79 if ![runto 'marker1'] then {
80     perror "couldn't run to marker1"
81     continue
82 }
83
84 gdb_test "up" ".*main.*" "up from marker1"
85
86 # Access a data item inside a namespace using colons and
87 # single quotes. :-(
88
89 # NOTE: carlton/2003-09-24: the quotes are becoming less necessary (or
90 # even desirable.)  For tests where it should still work with quotes,
91 # I'm including versions both with and without quotes; for tests that
92 # shouldn't work with quotes, I'm only including one version.
93
94 gdb_test "print 'AAA::c'" \
95     "\\$\[0-9\]* = 0 '\\\\(0|000)'" \
96     "print 'AAA::c'"
97
98 gdb_test "print AAA::c" \
99     "\\$\[0-9\]* = 0 '\\\\(0|000)'" \
100     "print AAA::c"
101
102 # An object declared using "using".
103
104 gdb_test "print ina" "\\$\[0-9\]+ = {xx = 33}"
105
106 gdb_test_multiple "ptype ina" "ptype ina" {
107     -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 $" {
108         pass "ptype ina"
109     }
110     -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 $" {
111         pass "ptype ina"
112     }
113 }
114
115 # Check all functions are known to GDB
116
117 setup_xfail hppa*-*-*11* CLLbs14869
118 gdb_test_multiple "info func xyzq" "info func xyzq" {
119     -re "All functions.*File.*namespace.cc:\r\nint AAA::A_xyzq\\(int\\);\r\nint BBB::B_xyzq\\(int\\);\r\nchar AAA::xyzq\\(char\\);\r\nchar BBB::xyzq\\(char\\);\r\nchar BBB::CCC::xyzq\\(char\\);\r\nchar BBB::Class::xyzq\\(char\\);\r\n$gdb_prompt $" {
120         pass "info func xyzq"
121     }    
122     -re "All functions.*File.*namespace.cc:\r\nint AAA::A_xyzq\\(int\\);\r\nchar AAA::xyzq\\(char\\);\r\nint BBB::B_xyzq\\(int\\);\r\nchar BBB::CCC::xyzq\\(char\\);\r\nchar BBB::Class::xyzq\\(char\\);\r\nchar BBB::xyzq\\(char\\);\r\n$gdb_prompt $" {
123         pass "info func xyzq"
124     }    
125 }
126
127 # Call a function in a namespace
128
129 gdb_test "print 'AAA::xyzq'('x')" \
130     "\\$\[0-9\]* = 97 'a'" \
131     "print 'AAA::xyzq'('x')"
132
133 gdb_test "print AAA::xyzq('x')" \
134     "\\$\[0-9\]* = 97 'a'" \
135     "print AAA::xyzq('x')"
136        
137 # Break on a function in a namespace
138
139 gdb_test "break AAA::xyzq" \
140     "Breakpoint.*at $hex: file.*namespace.cc, line 47\\."
141
142 # Break on a function in the global namespace.
143
144 gdb_test "break ::ensureOtherRefs" \
145     "Breakpoint.*at $hex: file.*$srcfile1, line $decimal\\."
146
147 # Call a function in a nested namespace
148
149 gdb_test "print 'BBB::CCC::xyzq'('x')" \
150     "\\$\[0-9\]* = 122 'z'" \
151     "print 'BBB::CCC::xyzq'('x')"
152
153 gdb_test "print BBB::CCC::xyzq('x')" \
154     "\\$\[0-9\]* = 122 'z'" \
155     "print BBB::CCC::xyzq('x')"
156        
157 # Break on a function in a nested namespace
158
159 gdb_test "break BBB::CCC::xyzq" \
160     "Breakpoint.*at $hex: file.*namespace.cc, line 63\\."
161
162 # Break on the same function, starting with the global namespace.
163
164 gdb_test "break ::BBB::CCC::xyzq" \
165     ".*Breakpoint.*at $hex: file.*$srcfile, line 63\\."
166
167 # Print address of a function in a class in a namespace
168
169 gdb_test "print 'BBB::Class::xyzq'" \
170     "\\$\[0-9\]* = \{char \\((BBB::|)Class \\*( const|), (char|int)\\)\} $hex <BBB::Class::xyzq\\(char\\)>" \
171     "print 'BBB::Class::xyzq'"
172
173 gdb_test "print BBB::Class::xyzq" \
174     "\\$\[0-9\]* = \{char \\((BBB::|)Class \\*( const|), (char|int)\\)\} $hex <BBB::Class::xyzq\\(char\\)>" \
175     "print BBB::Class::xyzq"
176
177 # Break on a function in a class in a namespace
178
179 gdb_test "break BBB::Class::xyzq" \
180     "Breakpoint.*at $hex: file.*namespace.cc, line 68\\."
181
182 # Tests accessing static elements in namespace of other file.
183
184 gdb_test "whatis C::cOtherFileType" "type = short"
185 gdb_test "whatis ::C::cOtherFileType" "type = short"
186 gdb_test "whatis C::cOtherFileVar" "type = const C::cOtherFileType"
187 gdb_test "whatis ::C::cOtherFileVar" "type = const C::cOtherFileType"
188 gdb_test "print C::cOtherFileVar" "\\$\[0-9\].* = 319"
189 gdb_test "print ::C::cOtherFileVar" "\\$\[0-9\].* = 319"
190
191 if $xfail_class_types { setup_xfail *-*-* }
192 gdb_test "whatis C::OtherFileClass::cOtherFileClassType" "type = short"
193 if $xfail_class_types { setup_xfail *-*-* }
194 gdb_test "whatis ::C::OtherFileClass::cOtherFileClassType" "type = short"
195 gdb_test "print C::OtherFileClass::cOtherFileClassVar" " = 318"
196
197 # FSF GCC <=4.4 creates unqualified DIE "cOtherFileClassVar" ignoring the
198 # namespace the same way older GDB did.
199 set test "print ::cOtherFileClassVar"
200 set test2 "print ::C::OtherFileClass::cOtherFileClassVar"
201 gdb_test_multiple $test $test {
202     -re "No symbol \"cOtherFileClassVar\" in current context\\.\r\n$gdb_prompt $" {
203         pass $test
204
205         gdb_test $test2 " = 318"
206     }
207     -re "\\$\[0-9\].* = 318\r\n$gdb_prompt $" {
208         # Do not permit to XFAIL on recent GCCs.
209         if $xfail_class_types {
210             setup_xfail *-*-* 
211             fail $test
212             # Unresolved means human intervention is required to determine
213             # whether the test passed or failed.  Since the previous test
214             # xfailed (not failed) human intervention isn't going to help here.
215             # Thus test2 is marked as unsupported instead of unresolved.
216             unsupported $test2
217         } else {
218             fail $test
219             unresolved $test2
220         }
221     }
222 }
223
224 # Test to see if the appropriate namespaces are in scope when trying
225 # to print out stuff from within a function defined within a
226 # namespace.
227
228 if ![runto "C::D::marker2"] then {
229     perror "couldn't run to marker2"
230     continue
231 }
232
233 gdb_test "print c" "\\$\[0-9\].* = 1"
234 gdb_test "print cc" "No symbol \"cc\" in current context."
235 gdb_test "print 'C::cc'" "\\$\[0-9\].* = 2"
236 gdb_test "print C::cc" "\\$\[0-9\].* = 2"
237 gdb_test "print cd" "\\$\[0-9\].* = 3"
238 gdb_test "print C::D::cd" "No type \"D\" within class or namespace \"C::C\"."
239 gdb_test "print 'E::cde'" "\\$\[0-9\].* = 5"
240 gdb_test "print E::cde" "\\$\[0-9\].* = 5"
241 gdb_test "print shadow" "\\$\[0-9\].* = 13"
242 gdb_test "print E::ce" "No symbol \"ce\" in namespace \"C::D::E\"."
243 gdb_test "ptype C" "type = namespace C::C"
244 gdb_test "ptype E" "type = namespace C::D::E"
245
246 gdb_test "ptype CClass" "type = (class C::CClass \{\r\n  public:|struct C::CClass \{)\r\n    int x;\r\n\}"
247 gdb_test "ptype CClass::NestedClass" "type = (class C::CClass::NestedClass \{\r\n  public:|struct C::CClass::NestedClass \{)\r\n    int y;\r\n\}"
248 gdb_test "ptype NestedClass" "No symbol \"NestedClass\" in current context."
249 gdb_test "ptype ::C::CClass" "type = class C::CClass \{\r\n  public:\r\n    int x;\r\n\}"
250 gdb_test "ptype ::C::CClass::NestedClass" "type = class C::CClass::NestedClass \{\r\n  public:\r\n    int y;\r\n\}"
251 gdb_test "ptype ::C::NestedClass" "No symbol \"NestedClass\" in namespace \"C\"."
252 gdb_test "ptype C::CClass" "No symbol \"CClass\" in namespace \"C::C\"."
253 gdb_test "ptype C::CClass::NestedClass" "No type \"CClass\" within class or namespace \"C::C\"."
254 gdb_test "ptype C::NestedClass" "No symbol \"NestedClass\" in namespace \"C::C\"."
255
256 # Tests involving multiple files
257
258 gdb_test "print cOtherFile" "\\$\[0-9\].* = 316"
259 gdb_test "ptype OtherFileClass" "type = (class C::OtherFileClass \{\r\n  public:|struct C::OtherFileClass \{)\r\n    int z;\r\n.*\}"
260 gdb_test "ptype ::C::OtherFileClass" "type = class C::OtherFileClass \{\r\n  public:\r\n    int z;\r\n.*\}"
261 gdb_test "ptype C::OtherFileClass" "No symbol \"OtherFileClass\" in namespace \"C::C\"."
262
263 # Test class typedefs printing.
264 set expect "type = class C::OtherFileClass \{\r\n.*\r\n *typedef short cOtherFileClassType;\r\n *typedef long cOtherFileClassType2;\r\n\}"
265 if $xfail_class_types { setup_xfail *-*-* }
266 gdb_test "ptype OtherFileClass" $expect "ptype OtherFileClass typedefs"
267 if $xfail_class_types { setup_xfail *-*-* }
268 gdb_test "ptype ::C::OtherFileClass" $expect "ptype ::C::OtherFileClass typedefs"
269
270 # Some anonymous namespace tests.
271
272 gdb_test "print cX" "\\$\[0-9\].* = 6"
273 gdb_test "print 'F::cXf'" "\\$\[0-9\].* = 7"
274 gdb_test "print F::cXf" "\\$\[0-9\].* = 7"
275 gdb_test "print F::cXfX" "\\$\[0-9\].* = 8"
276 gdb_test "print X" "\\$\[0-9\].* = 9"
277 gdb_test "print 'G::Xg'" "\\$\[0-9\].* = 10"
278 gdb_test "print G::Xg" "\\$\[0-9\].* = 10"
279 gdb_test "print G::XgX" "\\$\[0-9\].* = 11"
280 gdb_test "print cXOtherFile" "No symbol \"cXOtherFile\" in current context."
281 gdb_test "print XOtherFile" "No symbol \"XOtherFile\" in current context."
282
283 # Enum tests.
284 gdb_test "print AAA::ALPHA" "\\$\[0-9\].* = AAA::ALPHA"
285
286 # Regression tests for PR 9496.
287 gdb_test "whatis ::C::CClass::NestedClass" "type = C::CClass::NestedClass"
288 gdb_test "whatis ::C::CClass::NestedClass *" "type = C::CClass::NestedClass \\*"