aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.dwarf2/dw2-unusual-field-names.exp
blob: fd1ec1e106b7f038a919ff7bd2e0b5ed646a45e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright 2018-2020 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Test that GDB can support accesing fields of a structure if the
# fields have non-standard names.  Specifically, if the names are
# reserved C type names like 'double', 'float', 'int', etc.
#
# We don't expect to that such structures should be seen in real C
# code, but in some cases GDB will generate artificial structures, and
# in some cases, these type names are the obvious choice for field
# names.
#
# One specific example is RISC-V, the 64-bit floating point registers
# are represented as a structure with the field names 'float' and
# 'double'.

load_lib dwarf.exp

# This test can only be run on targets which support DWARF-2 and use
# gas.
if {![dwarf2_support]} {
    return 0
}

standard_testfile dw2-unusual-field-names.c dw2-unusual-field-names.S
set asm_file [standard_output_file $srcfile2]

# We need to know the size of integer and address types in order to
# write some of the debugging info we'd like to generate.
#
# For that, we ask GDB by debugging our test program.  Any program
# would do, but since we already have one specifically for this
# testcase, might as well use that.
if [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] {
    return -1
}
set int_size [get_sizeof "int" -1]

# Rebuild the test binary with the single field within the structure
# renamed to FIELD_NAME, then test that we can access the field
# through '.' and through '->'.
proc run_test { field_name } {
    global asm_file testfile srcfile subdir srcdir
    global int_size

    Dwarf::assemble $asm_file {
	global srcdir subdir srcfile
	global field_name int_size

	cu {} {
	    DW_TAG_compile_unit {
                {DW_AT_language @DW_LANG_C}
                {DW_AT_name     dw2-unusual-field-names.c}
                {DW_AT_comp_dir /tmp}
	    } {
		declare_labels itype ptype stype

		itype: DW_TAG_base_type {
		    {DW_AT_byte_size $int_size DW_FORM_sdata}
		    {DW_AT_encoding  @DW_ATE_signed}
		    {DW_AT_name int}
		}

		stype: DW_TAG_structure_type {
		    {DW_AT_name "foo"}
		    {DW_AT_byte_size $int_size DW_FORM_sdata}
		} {
		    member {
			{name $field_name}
			{type :$itype}
			{data_member_location 0 data1}
		    }
		}

		ptype: DW_TAG_pointer_type {
		    {DW_AT_type :$stype}
		}

		DW_TAG_variable {
		    {DW_AT_name obj}
		    {DW_AT_type :$stype}
		    {DW_AT_location {
			DW_OP_addr [gdb_target_symbol obj]
		    } SPECIAL_expr}
		    {external 1 flag}
		}

		DW_TAG_variable {
		    {DW_AT_name ptr}
		    {DW_AT_type :$ptype}
		    {DW_AT_location {
			DW_OP_addr [gdb_target_symbol ptr]
		    } SPECIAL_expr}
		    {external 1 flag}
		}
	    }
	}
    }

    if { [prepare_for_testing "failed to prepare" ${testfile} \
	      [list $srcfile $asm_file] {nodebug}] } {
	return -1
    }

    if ![runto_main] {
	return -1
    }

    gdb_test "p obj.$field_name" " = 0" \
	"access a field named '$field_name' directly"

    gdb_test "p ptr->$field_name" " = 0" \
	"access a field named '$field_name' through a pointer"

    gdb_exit
}

foreach field_name { double float char byte long int short unsigned signed } {
    run_test $field_name
}