1# Copyright 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2007
2# Free Software Foundation, 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
28if $tracelevel then {
29        strace $tracelevel
30        }
31
32set prms_id 0
33set bug_id 0
34
35if { [skip_cplus_tests] } { continue }
36
37set testfile "namespace"
38set srcfile ${testfile}.cc
39set objfile ${objdir}/${subdir}/${testfile}.o
40set srcfile1 ${testfile}1.cc
41set objfile1 ${objdir}/${subdir}/${testfile}1.o
42set binfile ${objdir}/${subdir}/${testfile}
43
44if [get_compiler_info ${binfile} c++] {
45    return -1;
46}
47
48if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${objfile}" object {debug c++}] != "" } {
49     untested namespace.exp
50     return -1
51}
52
53if  { [gdb_compile "${srcdir}/${subdir}/${srcfile1}" "${objfile1}" object {debug c++}] != "" } {
54     untested namespace.exp
55     return -1
56}
57
58if  { [gdb_compile "${objfile} ${objfile1}" "${binfile}" executable {debug c++}] != "" } {
59     untested namespace.exp
60     return -1
61}
62
63gdb_exit
64gdb_start
65gdb_reinitialize_dir $srcdir/$subdir
66gdb_load ${binfile}
67
68
69#
70# set it up at a breakpoint so we can play with the variable values
71#
72if ![runto_main] then {
73    perror "couldn't run to breakpoint"
74    continue
75}
76
77if ![runto 'marker1'] then {
78    perror "couldn't run to marker1"
79    continue
80}
81
82gdb_test "up" ".*main.*" "up from marker1"
83
84# Access a data item inside a namespace using colons and
85# single quotes. :-(
86
87# NOTE: carlton/2003-09-24: the quotes are becoming less necessary (or
88# even desirable.)  For tests where it should still work with quotes,
89# I'm including versions both with and without quotes; for tests that
90# shouldn't work with quotes, I'm only including one version.
91
92send_gdb "print 'AAA::c'\n"
93gdb_expect {
94   -re "\\$\[0-9\]* = 0 '\\\\(0|000)'\r\n$gdb_prompt $" { pass "print 'AAA::c'" }
95   -re ".*$gdb_prompt $" { fail "print 'AAA::c'" }
96   timeout { fail "(timeout) print 'AAA::c'" }
97}
98
99send_gdb "print AAA::c\n"
100gdb_expect {
101   -re "\\$\[0-9\]* = 0 '\\\\(0|000)'\r\n$gdb_prompt $" { pass "print AAA::c" }
102   -re ".*$gdb_prompt $" { fail "print AAA::c" }
103   timeout { fail "(timeout) print AAA::c" }
104}
105
106# An object declared using "using".
107
108send_gdb "print ina\n"
109gdb_expect {
110   -re "\\$\[0-9\]+ = {xx = 33}.*$gdb_prompt $" {
111      pass "print ina"
112   }
113   -re ".*$gdb_prompt $" { fail "print ina" }
114   timeout { fail "(timeout) print ina" }
115}
116
117send_gdb "ptype ina\n"
118gdb_expect {
119   -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 $" {
120       pass "ptype ina"
121   }
122   -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 $" {
123       pass "ptype ina"
124   }
125   -re ".*$gdb_prompt $" { fail "ptype ina" }
126   timeout { fail "(timeout) ptype ina" }
127}
128
129# Check all functions are known to GDB
130
131setup_xfail hppa*-*-*11* CLLbs14869
132send_gdb "info func xyzq\n"
133gdb_expect {
134   -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 $" {
135       pass "info func xyzq"
136   }
137   -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 $" {
138       pass "info func xyzq"
139   }
140   -re ".*$gdb_prompt $" { fail "info func xyzq" }
141   timeout { fail "(timeout) info func xyzq" }
142}
143
144# Call a function in a namespace
145
146send_gdb "print 'AAA::xyzq'('x')\n"
147gdb_expect {
148   -re "\\$\[0-9\]* = 97 'a'\r\n$gdb_prompt $" {
149       pass "print 'AAA::xyzq'('x')"
150   }
151   -re ".*$gdb_prompt $" { fail "print 'AAA::xyzq'('x')" }
152   timeout { fail "(timeout) print 'AAA::xyzq'('x')" }
153}
154
155send_gdb "print AAA::xyzq('x')\n"
156gdb_expect {
157   -re "\\$\[0-9\]* = 97 'a'\r\n$gdb_prompt $" {
158       pass "print AAA::xyzq('x')"
159   }
160   -re ".*$gdb_prompt $" { fail "print AAA::xyzq('x')" }
161   timeout { fail "(timeout) print AAA::xyzq('x')" }
162}
163
164# Break on a function in a namespace
165
166send_gdb "break AAA::xyzq\n"
167gdb_expect {
168    -re "Breakpoint.*at $hex: file.*namespace.cc, line 42\\.\r\n$gdb_prompt $" {
169         pass "break AAA::xyzq"
170    }
171   -re ".*$gdb_prompt $" { fail "break AAA::xyzq" }
172   timeout { fail "(timeout) break AAA::xyzq" }
173}
174
175# Call a function in a nested namespace
176
177send_gdb "print 'BBB::CCC::xyzq'('x')\n"
178gdb_expect {
179   -re "\\$\[0-9\]* = 122 'z'\r\n$gdb_prompt $" {
180       pass "print 'BBB::CCC::xyzq'('x')"
181   }
182   -re ".*$gdb_prompt $" { fail "print 'BBB::CCC::xyzq'('x')" }
183   timeout { fail "(timeout) print 'BBB::CCC::xyzq'('x')" }
184}
185
186send_gdb "print BBB::CCC::xyzq('x')\n"
187gdb_expect {
188   -re "\\$\[0-9\]* = 122 'z'\r\n$gdb_prompt $" {
189       pass "print BBB::CCC::xyzq('x')"
190   }
191   -re ".*$gdb_prompt $" { fail "print BBB::CCC::xyzq('x')" }
192   timeout { fail "(timeout) print BBB::CCC::xyzq('x')" }
193}
194
195# Break on a function in a nested namespace
196
197send_gdb "break BBB::CCC::xyzq\n"
198gdb_expect {
199    -re "Breakpoint.*at $hex: file.*namespace.cc, line 58\\.\r\n$gdb_prompt $" {
200         pass "break BBB::CCC::xyzq"
201    }
202   -re ".*$gdb_prompt $" { fail "break BBB::CCC::xyzq" }
203   timeout { fail "(timeout) break BBB::CCC::xyzq" }
204}
205
206# Print address of a function in a class in a namespace
207
208send_gdb "print 'BBB::Class::xyzq'\n"
209gdb_expect {
210   -re "\\$\[0-9\]* = \{char \\((BBB::|)Class \\*( const|), (char|int)\\)\} $hex <BBB::Class::xyzq\\(char\\)>\r\n$gdb_prompt $" {
211       pass "print 'BBB::Class::xyzq'"
212   }
213   -re ".*$gdb_prompt $" { fail "print 'BBB::Class::xyzq'" }
214   timeout { fail "(timeout) print 'BBB::Class::xyzq'" }
215}
216
217send_gdb "print BBB::Class::xyzq\n"
218gdb_expect {
219   -re "\\$\[0-9\]* = \{char \\((BBB::|)Class \\*( const|), (char|int)\\)\} $hex <BBB::Class::xyzq\\(char\\)>\r\n$gdb_prompt $" {
220       pass "print BBB::Class::xyzq"
221   }
222   -re ".*$gdb_prompt $" { fail "print BBB::Class::xyzq" }
223   timeout { fail "(timeout) print BBB::Class::xyzq" }
224}
225
226# Break on a function in a class in a namespace
227
228send_gdb "break BBB::Class::xyzq\n"
229gdb_expect {
230    -re "Breakpoint.*at $hex: file.*namespace.cc, line 63\\.\r\n$gdb_prompt $" {
231         pass "break BBB::Class::xyzq"
232    }
233   -re ".*$gdb_prompt $" { fail "break BBB::Class::xyzq" }
234   timeout { fail "(timeout) break BBB::Class::xyzq" }
235}
236
237# Test to see if the appropriate namespaces are in scope when trying
238# to print out stuff from within a function defined within a
239# namespace.
240
241if ![runto "C::D::marker2"] then {
242    perror "couldn't run to marker2"
243    continue
244}
245
246gdb_test "print c" "\\$\[0-9\].* = 1"
247gdb_test "print cc" "No symbol \"cc\" in current context."
248gdb_test "print 'C::cc'" "\\$\[0-9\].* = 2"
249gdb_test "print C::cc" "\\$\[0-9\].* = 2"
250gdb_test "print cd" "\\$\[0-9\].* = 3"
251gdb_test "print C::D::cd" "No type \"D\" within class or namespace \"C::C\"."
252gdb_test "print 'E::cde'" "\\$\[0-9\].* = 5"
253gdb_test "print E::cde" "\\$\[0-9\].* = 5"
254gdb_test "print shadow" "\\$\[0-9\].* = 13"
255gdb_test "print E::ce" "No symbol \"ce\" in namespace \"C::D::E\"."
256gdb_test "ptype C" "type = namespace C::C"
257gdb_test "ptype E" "type = namespace C::D::E"
258
259gdb_test "ptype CClass" "type = (class C::CClass \{\r\n  public:|struct C::CClass \{)\r\n    int x;\r\n\}"
260gdb_test "ptype CClass::NestedClass" "type = (class C::CClass::NestedClass \{\r\n  public:|struct C::CClass::NestedClass \{)\r\n    int y;\r\n\}"
261gdb_test "ptype NestedClass" "No symbol \"NestedClass\" in current context."
262setup_kfail "gdb/1448" "*-*-*"
263gdb_test "ptype ::C::CClass" "type = class C::CClass \{\r\n  public:\r\n    int x;\r\n\}"
264setup_kfail "gdb/1448" "*-*-*"
265gdb_test "ptype ::C::CClass::NestedClass" "type = class C::CClass::NestedClass \{\r\n  public:\r\n    int y;\r\n\}"
266setup_kfail "gdb/1448" "*-*-*"
267gdb_test "ptype ::C::NestedClass" "No symbol \"NestedClass\" in namespace \"C\"."
268gdb_test "ptype C::CClass" "No symbol \"CClass\" in namespace \"C::C\"."
269gdb_test "ptype C::CClass::NestedClass" "No type \"CClass\" within class or namespace \"C::C\"."
270gdb_test "ptype C::NestedClass" "No symbol \"NestedClass\" in namespace \"C::C\"."
271
272# Tests involving multiple files
273
274gdb_test "print cOtherFile" "\\$\[0-9\].* = 316"
275gdb_test "ptype OtherFileClass" "type = (class C::OtherFileClass \{\r\n  public:|struct C::OtherFileClass \{)\r\n    int z;\r\n\}"
276setup_kfail "gdb/1448" "*-*-*"
277gdb_test "ptype ::C::OtherFileClass" "type = class C::OtherFileClass \{\r\n  public:\r\n    int z;\r\n\}"
278gdb_test "ptype C::OtherFileClass" "No symbol \"OtherFileClass\" in namespace \"C::C\"."
279
280# Some anonymous namespace tests.
281
282gdb_test "print cX" "\\$\[0-9\].* = 6"
283gdb_test "print 'F::cXf'" "\\$\[0-9\].* = 7"
284gdb_test "print F::cXf" "\\$\[0-9\].* = 7"
285gdb_test "print F::cXfX" "\\$\[0-9\].* = 8"
286gdb_test "print X" "\\$\[0-9\].* = 9"
287gdb_test "print 'G::Xg'" "\\$\[0-9\].* = 10"
288gdb_test "print G::Xg" "\\$\[0-9\].* = 10"
289gdb_test "print G::XgX" "\\$\[0-9\].* = 11"
290gdb_test "print cXOtherFile" "No symbol \"cXOtherFile\" in current context."
291gdb_test "print XOtherFile" "No symbol \"XOtherFile\" in current context."
292