1 # Copyright 2013-2024 Free Software Foundation, Inc.
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.
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.
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/>.
16 # Test namespace aliases.
17 # PRs c++/7935, c++/10541
21 require dwarf2_support allow_cplus_tests
23 standard_testfile .cc nsalias-dw.S
25 # Make the DWARF used for the test. This is necessary to work
26 # around compiler issues. Not all versions of gcc output the
27 # correct debuginfo we need.
29 # This should create the equivalent DWARF to:
38 # int foo (void) { return x; }
41 # namespace Innermost = innermost;
44 # int foo (void) { return x + Innermost::foo (); }
47 # namespace Inner = inner;
50 # int foo (void) { return x + Inner::foo (); }
53 # namespace Outer = outer;
54 # namespace oi = Outer::Inner;
56 set asm_file [standard_output_file $srcfile2]
57 Dwarf::assemble $asm_file {
59 compile_unit {{language @DW_LANG_C_plus_plus}} {
60 declare_labels int_label outer_label inner_label innermost_label
61 declare_labels im_foo_label i_foo_label o_foo_label
62 declare_labels OuterInner_label oi1_label oi2_label
64 int_label: base_type {
66 {encoding @DW_ATE_signed}
67 {byte_size 4 DW_FORM_sdata}
70 outer_label: DW_TAG_namespace {
73 inner_label: DW_TAG_namespace {
76 innermost_label: DW_TAG_namespace {
82 {const_value 2 DW_FORM_data1}
85 im_foo_label: DW_TAG_subprogram {
87 {external 1 flag_present}
88 {declaration 1 flag_present}
92 imported_declaration {
94 {import :$innermost_label}
100 {const_value 1 DW_FORM_data1}
103 i_foo_label: subprogram {
105 {external 1 flag_present}
106 {declaration 1 flag_present}
110 OuterInner_label: imported_declaration {
112 {import :$inner_label}
118 {const_value 0 DW_FORM_data1}
121 o_foo_label: subprogram {
123 {external 1 flag_present}
124 {declaration 1 flag_present}
128 imported_declaration {
130 {import :$outer_label}
133 oi1_label: imported_declaration {
135 {import :$OuterInner_label}
138 oi2_label: imported_declaration {
143 imported_declaration {
149 {specification :$im_foo_label}
150 {low_pc 0x4 DW_FORM_addr}
151 {high_pc 0x7 DW_FORM_addr}
155 {specification :$i_foo_label}
156 {low_pc 0x8 DW_FORM_addr}
157 {high_pc 0xb DW_FORM_addr}
161 {specification :$o_foo_label}
162 {low_pc 0xc DW_FORM_addr}
163 {high_pc 0xf DW_FORM_addr}
169 if {[gdb_compile $srcdir/$subdir/$srcfile ${binfile}1.o \
170 object {c++ debug}] != ""} {
174 if {[gdb_compile $asm_file ${binfile}2.o object {nodebug}] != ""} {
178 if {[gdb_compile [list ${binfile}1.o ${binfile}2.o] \
179 $binfile executable {c++}] != ""} {
183 clean_restart $testfile
185 # A procedure to run various tests on aliased namespaces.
186 proc do_alias_tests {ns {real ""} {x ""}} {
188 # The "real" namespace is simply NS in all lowercase.
190 set real [string tolower $ns]
193 # The value of `x' is the number of '::' in NS.
195 set x [expr {[llength [split $ns ":"]] / 2}]
199 gdb_test "whatis $ns" "type = $real"
202 gdb_test "ptype $ns" "type = namespace $real"
205 send_log "expecting x = $x\n"
206 gdb_test "print ${ns}::x" " = $x"
208 # Attempt to list the function.
209 gdb_test_no_output "list ${ns}::foo"
211 # Attempt to break on the start of the function.
212 gdb_breakpoint "*${ns}::foo"
215 with_test_prefix "($ns)" {
216 gdb_test_no_output "delete \$bpnum"
220 # This is a list of all the permutations to be tested. For troubleshooting
221 # purposes, this list is explicitly enumerated.
224 lappend permutations "outer"
225 lappend permutations "Outer"
226 lappend permutations "outer::inner"
227 lappend permutations "Outer::inner"
228 lappend permutations "outer::Inner"
229 lappend permutations "Outer::Inner"
230 lappend permutations "outer::inner::innermost"
231 lappend permutations "outer::inner::Innermost"
232 lappend permutations "outer::Inner::innermost"
233 lappend permutations "outer::Inner::Innermost"
234 lappend permutations "Outer::inner::innermost"
235 lappend permutations "Outer::inner::Innermost"
236 lappend permutations "Outer::Inner::innermost"
237 lappend permutations "Outer::Inner::Innermost"
239 foreach p $permutations {
243 # Test recursively imported aliases.
244 foreach ns {"oi1" "oi2" "oi3"} {
245 do_alias_tests $ns "outer::inner" 1
246 do_alias_tests "${ns}::innermost" "outer::inner::innermost" 2
247 do_alias_tests "${ns}::Innermost" "outer::inner::innermost" 2
250 # Generate another objfile with nested imported declarations.
253 declare_labels n0_label
255 n0_label: DW_TAG_namespace {
261 {const_value 3 DW_FORM_data1}
265 declare_labels n0_import
266 n0_import: imported_declaration {
272 for {set i 1} {$i <= 100} {incr i} {
273 append imports [format "
274 declare_labels n%d_import
275 n%d_import: imported_declaration {
277 {import :\$n%d_import}
278 }" $i $i $i [expr {$i - 1}]]
281 standard_testfile .cc nsalias-r-dw.S
283 set asm_file [standard_output_file $srcfile2]
284 set the_dwarf [format {
286 compile_unit {{language @DW_LANG_C_plus_plus}} {
287 declare_labels int_label n0_label
289 int_label: base_type {
291 {encoding @DW_ATE_signed}
292 {byte_size 4 DW_FORM_sdata}
300 Dwarf::assemble $asm_file $the_dwarf
302 if {[gdb_compile $asm_file ${binfile}3.o object {nodebug}] != ""} {
306 if {[gdb_compile [list ${binfile}1.o ${binfile}3.o] \
307 ${binfile}-r executable {c++}] != ""} {
313 # Set complaints before loading the file. Otherwise the complaint won't
314 # trigger for -readnow.
315 gdb_test_no_output "set complaints 1"
317 gdb_load [standard_output_file ${testfile}-r]
319 set readnow_p [readnow]
321 set test "complaint for too many recursively imported declarations"
322 set re ".* has too many recursively imported declarations.*"
324 global gdb_file_cmd_msg
325 gdb_assert {[regexp $re $gdb_file_cmd_msg]} $test
327 gdb_test "print N100::x" $re $test