]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.dwarf2/implref-const.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.dwarf2 / implref-const.exp
CommitLineData
1d506c26 1# Copyright 2016-2024 Free Software Foundation, Inc.
a22df60a
MG
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
3326303b
MG
16# Test a C++ reference marked with DW_OP_GNU_implicit_pointer.
17# The referenced value is a DW_AT_const_value.
a22df60a 18
0b94d2b9 19require allow_cplus_tests
a22df60a
MG
20
21load_lib dwarf.exp
22
23# This test can only be run on targets which support DWARF-2 and use gas.
ce8d533e 24require dwarf2_support
a22df60a 25
3326303b 26# We'll place the output of Dwarf::assemble in implref-const.S.
a22df60a
MG
27standard_testfile main.c .S
28
3326303b 29# ${testfile} is now "implref-const". srcfile2 is "implref-const.S".
a22df60a
MG
30set executable ${testfile}
31set asm_file [standard_output_file ${srcfile2}]
32
3326303b
MG
33# We need to know the size of integer and address types in order
34# to write some of the debugging info we'd like to generate.
35#
36# For that, we ask GDB by debugging our implref-const program.
37# Any program would do, but since we already have implref-const
38# specifically for this testcase, might as well use that.
5b362f04 39if [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] {
3326303b
MG
40 return -1
41}
42
a22df60a
MG
43# Create the DWARF. We need a regular variable and a reference to it that'll
44# be marked with DW_OP_GNU_implicit_pointer.
3326303b 45Dwarf::assemble ${asm_file} {
3326303b 46 cu {} {
a22df60a 47 DW_TAG_compile_unit {
a22df60a
MG
48 {DW_AT_language @DW_LANG_C_plus_plus}
49 } {
50 declare_labels int_label const_label variable_label ref_label
3326303b
MG
51 set int_size [get_sizeof "int" -1]
52
53 # gdb always assumes references are implemented as pointers.
54 set addr_size [get_sizeof "void *" -1]
55 set var_value 42
a22df60a
MG
56
57 int_label: DW_TAG_base_type {
3326303b 58 {DW_AT_byte_size ${int_size} DW_FORM_udata}
a22df60a
MG
59 {DW_AT_encoding @DW_ATE_signed}
60 {DW_AT_name "int"}
61 }
62
63 ref_label: DW_TAG_reference_type {
3326303b 64 {DW_AT_byte_size ${addr_size} DW_FORM_udata}
a22df60a
MG
65 {DW_AT_type :${int_label}}
66 }
67
68 const_label: DW_TAG_const_type {
69 {DW_AT_type :${ref_label}}
70 }
71
72 DW_TAG_subprogram {
10da644d 73 {MACRO_AT_func { "main" }}
a22df60a
MG
74 {DW_AT_type :${int_label}}
75 {DW_AT_external 1 DW_FORM_flag}
76 } {
77 variable_label: DW_TAG_variable {
78 {DW_AT_name "var"}
79 {DW_AT_type :${int_label}}
3326303b 80 {DW_AT_const_value ${var_value} DW_FORM_udata}
a22df60a
MG
81 }
82
83 DW_TAG_variable {
84 {DW_AT_name "ref"}
85 {DW_AT_type :${const_label}}
86 {DW_AT_location {DW_OP_GNU_implicit_pointer ${variable_label} 0} SPECIAL_expr}
87 }
88 }
89 }
90 }
91}
92
5b362f04 93if [prepare_for_testing "failed to prepare" ${executable} [list ${asm_file} ${srcfile}] {}] {
a22df60a
MG
94 return -1
95}
96
97# DW_OP_GNU_implicit_pointer implementation requires a valid frame.
98if ![runto_main] {
99 return -1
100}
101
3326303b
MG
102# Doing 'print ref' should show us e.g. '(int &) <synthetic pointer>: 42'.
103gdb_test "print ref" " = \\(int &\\) <synthetic pointer>: \\\d+"
104
105# The variable isn't located in memory, thus we can't take its address.
106gdb_test "print &var" "Can't take address of \"var\" which isn't an lvalue."
107gdb_test "print &ref" "Attempt to take address of value not located in memory."
a22df60a
MG
108
109# gdb assumes C++ references are implemented as pointers, and print &(&ref)
110# shows us the underlying pointer's address.
111# Since in this case there's no physical pointer, gdb should tell us so.
3326303b 112gdb_test "print &(&ref)" "Attempt to take address of value not located in memory."