Automatic date update in version.in
[binutils-gdb/blckswan.git] / gdb / testsuite / gdb.dwarf2 / implref-const.exp
blobc25fc7e9bf3ea414f1872ea324be008ce7261b43
1 # Copyright 2016-2022 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 a C++ reference marked with DW_OP_GNU_implicit_pointer.
17 # The referenced value is a DW_AT_const_value.
19 if [skip_cplus_tests] {
20 return
23 load_lib dwarf.exp
25 # This test can only be run on targets which support DWARF-2 and use gas.
26 if ![dwarf2_support] {
27 return 0
30 # We'll place the output of Dwarf::assemble in implref-const.S.
31 standard_testfile main.c .S
33 # ${testfile} is now "implref-const". srcfile2 is "implref-const.S".
34 set executable ${testfile}
35 set asm_file [standard_output_file ${srcfile2}]
37 # We need to know the size of integer and address types in order
38 # to write some of the debugging info we'd like to generate.
40 # For that, we ask GDB by debugging our implref-const program.
41 # Any program would do, but since we already have implref-const
42 # specifically for this testcase, might as well use that.
43 if [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] {
44 return -1
47 # Create the DWARF. We need a regular variable and a reference to it that'll
48 # be marked with DW_OP_GNU_implicit_pointer.
49 Dwarf::assemble ${asm_file} {
50 cu {} {
51 DW_TAG_compile_unit {
52 {DW_AT_language @DW_LANG_C_plus_plus}
53 } {
54 declare_labels int_label const_label variable_label ref_label
55 set int_size [get_sizeof "int" -1]
57 # gdb always assumes references are implemented as pointers.
58 set addr_size [get_sizeof "void *" -1]
59 set var_value 42
61 int_label: DW_TAG_base_type {
62 {DW_AT_byte_size ${int_size} DW_FORM_udata}
63 {DW_AT_encoding @DW_ATE_signed}
64 {DW_AT_name "int"}
67 ref_label: DW_TAG_reference_type {
68 {DW_AT_byte_size ${addr_size} DW_FORM_udata}
69 {DW_AT_type :${int_label}}
72 const_label: DW_TAG_const_type {
73 {DW_AT_type :${ref_label}}
76 DW_TAG_subprogram {
77 {MACRO_AT_func { "main" }}
78 {DW_AT_type :${int_label}}
79 {DW_AT_external 1 DW_FORM_flag}
80 } {
81 variable_label: DW_TAG_variable {
82 {DW_AT_name "var"}
83 {DW_AT_type :${int_label}}
84 {DW_AT_const_value ${var_value} DW_FORM_udata}
87 DW_TAG_variable {
88 {DW_AT_name "ref"}
89 {DW_AT_type :${const_label}}
90 {DW_AT_location {DW_OP_GNU_implicit_pointer ${variable_label} 0} SPECIAL_expr}
97 if [prepare_for_testing "failed to prepare" ${executable} [list ${asm_file} ${srcfile}] {}] {
98 return -1
101 # DW_OP_GNU_implicit_pointer implementation requires a valid frame.
102 if ![runto_main] {
103 return -1
106 # Doing 'print ref' should show us e.g. '(int &) <synthetic pointer>: 42'.
107 gdb_test "print ref" " = \\(int &\\) <synthetic pointer>: \\\d+"
109 # The variable isn't located in memory, thus we can't take its address.
110 gdb_test "print &var" "Can't take address of \"var\" which isn't an lvalue."
111 gdb_test "print &ref" "Attempt to take address of value not located in memory."
113 # gdb assumes C++ references are implemented as pointers, and print &(&ref)
114 # shows us the underlying pointer's address.
115 # Since in this case there's no physical pointer, gdb should tell us so.
116 gdb_test "print &(&ref)" "Attempt to take address of value not located in memory."