Add $_memeq, $_regex, $_streq, $_strlen convenience functions.
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-type.exp
CommitLineData
c5a57081 1# Copyright (C) 2009-2012 Free Software Foundation, Inc.
bfd31e71
PM
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
16# This file is part of the GDB testsuite. It tests the mechanism
17# of exposing types to Python.
18
a2c09bd0
DE
19load_lib gdb-python.exp
20
b4a58790 21standard_testfile
bfd31e71 22
4c93b1db 23if [get_compiler_info c++] {
999adef4
DE
24 return -1;
25}
26
bfd31e71 27# Build inferior to language specification.
db8e4570
UW
28proc build_inferior {exefile lang} {
29 global srcdir subdir srcfile testfile hex
bfd31e71 30
db8e4570 31 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" executable "debug $lang"] != "" } {
bfd31e71
PM
32 untested "Couldn't compile ${srcfile} in $lang mode"
33 return -1
34 }
35}
36
f6bbabf0 37# Restart GDB.
db8e4570
UW
38proc restart_gdb {exefile} {
39 global srcdir subdir srcfile testfile hex
bfd31e71
PM
40
41 gdb_exit
42 gdb_start
43 gdb_reinitialize_dir $srcdir/$subdir
db8e4570 44 gdb_load ${exefile}
bfd31e71
PM
45
46 if ![runto_main ] then {
47 perror "couldn't run to breakpoint"
48 return
49 }
f6bbabf0 50}
bfd31e71 51
f6bbabf0
PM
52# Set breakpoint and run to that breakpoint.
53proc runto_bp {bp} {
bfd31e71
PM
54 gdb_breakpoint [gdb_get_line_number $bp]
55 gdb_continue_to_breakpoint $bp
56}
57
bfd31e71
PM
58proc test_fields {lang} {
59 global gdb_prompt
60
f6b47be4
DE
61 # .fields() of a typedef should still return the underlying field list
62 gdb_test "python print len(gdb.parse_and_eval('ts').type.fields())" "2" \
63 "$lang typedef field list"
64
bfd31e71
PM
65 if {$lang == "c++"} {
66 # Test usage with a class
67 gdb_py_test_silent_cmd "print c" "print value" 1
68 gdb_py_test_silent_cmd "python c = gdb.history (0)" "get value from history" 1
69 gdb_py_test_silent_cmd "python fields = c.type.fields()" "get fields" 1
70 gdb_test "python print len(fields)" "2" "Check number of fields"
71 gdb_test "python print fields\[0\].name" "c" "Check class field c name"
72 gdb_test "python print fields\[1\].name" "d" "Check class field d name"
d839c8a4
TT
73
74 gdb_test "python print c.type == gdb.parse_and_eval('d').type" "False"
75 gdb_test "python print c.type == gdb.parse_and_eval('d').type.fields()\[0\].type" \
76 "True"
bfd31e71
PM
77 }
78
79 # Test normal fields usage in structs.
80 gdb_py_test_silent_cmd "print st" "print value" 1
81 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1
82 gdb_py_test_silent_cmd "python fields = st.type.fields()" "get fields" 1
83 gdb_test "python print len(fields)" "2" "Check number of fields"
84 gdb_test "python print fields\[0\].name" "a" "Check structure field a name"
85 gdb_test "python print fields\[1\].name" "b" "Check structure field b name"
86
2e8265fd
TT
87 # Regression test for
88 # http://sourceware.org/bugzilla/show_bug.cgi?id=12070.
89 gdb_test "python print 'type' in dir(fields\[0\])" "True" \
90 "Check that dir includes name"
91
7a81bdbf
PK
92 # Test Python mapping behavior of gdb.Type for structs/classes
93 gdb_test "python print len(st.type)" "2" "Check number of fields"
94 gdb_test "python print st.type\['a'\].name" "a" "Check fields lookup by name"
95 gdb_test "python print \[v.bitpos for v in st.type.itervalues()\]" {\[0L, 32L\]} "Check fields iteration over values"
96 gdb_test "python print \[(n, v.bitpos) for (n, v) in st.type.items()\]" {\[\('a', 0L\), \('b', 32L\)\]} "Check fields items list"
97 gdb_test "python print 'a' in st.type" "True" "Check field name exists test"
98 gdb_test "python print 'nosuch' in st.type" "False" "Check field name nonexists test"
77426730
PK
99 gdb_test "python print not not st.type" "True" "Check conversion to bool"
100
101 # Test rejection of mapping operations on scalar types
102 gdb_test "python print len (st.type\['a'\].type)" "TypeError: Type is not a structure, union, or enum type.*"
103 gdb_test "python print st.type\['a'\].type.has_key ('x')" "TypeError: Type is not a structure, union, or enum type.*"
104 gdb_test "python print st.type\['a'\].type.keys ()" "TypeError: Type is not a structure, union, or enum type.*"
105 gdb_test "python print st.type\['a'\].type\['x'\]" "TypeError: Type is not a structure, union, or enum type.*"
106
107 # Test conversion to bool on scalar types
108 gdb_test "python print not not st.type\['a'\].type" "True"
7a81bdbf 109
bfd31e71
PM
110 # Test regression PR python/10805
111 gdb_py_test_silent_cmd "print ar" "print value" 1
112 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
113 gdb_test "python fields = ar.type.fields()"
114 gdb_test "python print len(fields)" "1" "Check the number of fields"
115 gdb_test "python print fields\[0\].type" "<range type>" "Check array field type"
702c2711 116
a72c3253 117 # Test gdb.Type.array.
702c2711
TT
118 gdb_test "python print ar\[0\].cast(ar\[0\].type.array(1))" \
119 ".1, 2." "cast to array with one argument"
120 gdb_test "python print ar\[0\].cast(ar\[0\].type.array(0, 1))" \
121 ".1, 2." "cast to array with two arguments"
d839c8a4
TT
122
123 gdb_test "python print ar\[0\].type == ar\[0\].type" "True"
a72c3253
DE
124
125 # Test gdb.Type.vector.
126 # Note: vectors cast differently than arrays. Here ar[0] is replicated
127 # for the size of the vector.
128 gdb_py_test_silent_cmd \
129 "python vec1 = ar\[0\].cast(ar\[0\].type.vector(1))" "set vec1" 1
130 gdb_test "python print vec1" ".1, 1." "cast to vector with one argument"
131 gdb_py_test_silent_cmd \
132 "python vec2 = ar\[0\].cast(ar\[0\].type.vector(0, 1))" "set vec2" 1
133 gdb_test "python print vec2" ".1, 1." "cast to vector with two arguments"
134 gdb_test "python print vec1 == vec2" "True"
135 gdb_py_test_silent_cmd \
136 "python vec3 = ar\[1\].cast(ar\[1\].type.vector(1))" "set vec3" 1
137 gdb_test "python print vec1 == vec3" "False"
bfd31e71
PM
138}
139
7a81bdbf
PK
140proc test_enums {} {
141 gdb_py_test_silent_cmd "print e" "print value" 1
142 gdb_py_test_silent_cmd "python e = gdb.history (0)" "get value from history" 1
143 gdb_py_test_silent_cmd "python fields = e.type.fields()" "get value from history" 1
144 gdb_test "python print len(fields)" "3" "Check the number of enum fields"
145 gdb_test "python print fields\[0\].name" "v1" "Check enum field name"
146 gdb_test "python print fields\[1\].name" "v2" "Check enum field name"
147
148 # Ditto but by mapping operations
149 gdb_test "python print len(e.type)" "3" "Check the number of enum fields"
150 gdb_test "python print e.type\['v1'\].name" "v1" "Check enum field lookup by name"
151 gdb_test "python print e.type\['v3'\].name" "v3" "Check enum field lookup by name"
14e75d8e
JK
152 gdb_test "python print \[v.enumval for v in e.type.itervalues()\]" {\[0L, 1L, 2L\]} "Check num fields iteration over values"
153 gdb_test "python print \[(n, v.enumval) for (n, v) in e.type.items()\]" {\[\('v1', 0L\), \('v2', 1L\), \('v3', 2L\)\]} "Check enum fields items list"
7a81bdbf 154}
bfd31e71
PM
155proc test_base_class {} {
156 gdb_py_test_silent_cmd "print d" "print value" 1
157 gdb_py_test_silent_cmd "python d = gdb.history (0)" "get value from history" 1
158 gdb_py_test_silent_cmd "python fields = d.type.fields()" "get value from history" 1
159 gdb_test "python print len(fields)" "3" "Check the number of fields"
160 gdb_test "python print fields\[0\].is_base_class" "True" "Check base class"
161 gdb_test "python print fields\[1\].is_base_class" "False" "Check base class"
162}
163
361ae042
PM
164proc test_range {} {
165
166 # Test a valid range request.
167 gdb_py_test_silent_cmd "print ar" "print value" 1
168 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
169 gdb_test "python print len(ar.type.range())" "2" "Check correct tuple length"
170 gdb_test "python print ar.type.range()\[0\]" "0" "Check low range"
171 gdb_test "python print ar.type.range()\[1\]" "1" "Check high range"
172
173 # Test a range request on a ranged type.
174 gdb_py_test_silent_cmd "print ar" "print value" 1
175 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
176 gdb_py_test_silent_cmd "python fields = ar.type.fields()" "get fields" 1
177 gdb_test "python print fields\[0\].type.range()\[0\]" "0" "Check range type low bound"
178 gdb_test "python print fields\[0\].type.range()\[1\]" "1" "Check range type high bound"
179
180 # Test where a range does not exist.
181 gdb_py_test_silent_cmd "print st" "print value" 1
182 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1
183 gdb_test "python print st.type.range()" "RuntimeError: This type does not have a range.*" "Check range for non ranged type."
184}
185
326fd672
TT
186# Some tests of template arguments.
187proc test_template {} {
188 gdb_py_test_silent_cmd \
189 "python ttype = gdb.parse_and_eval('temvar').type" \
190 "get type of temvar" \
191 1
192
193 gdb_test "python print ttype.template_argument(0)" "D"
194 gdb_test "python print isinstance(ttype.template_argument(0), gdb.Type)" \
195 "True"
999adef4 196
326fd672 197 # The next two tests require a GCC that emits DW_TAG_template_*.
999adef4
DE
198 # GCC 4.4 does not emit it, 4.5 and 6 do emit it.
199 set have_older_gcc 0
200 if {[test_compiler_info {gcc-[0-3]-*}]
201 || [test_compiler_info {gcc-4-[0-4]-*}]} {
202 set have_older_gcc 1
203 }
204 if $have_older_gcc { setup_xfail *-*-* }
326fd672 205 gdb_test "python print ttype.template_argument(1)" "23"
999adef4 206 if $have_older_gcc { setup_xfail *-*-* }
326fd672
TT
207 gdb_test "python print isinstance(ttype.template_argument(1), gdb.Value)" \
208 "True"
999adef4 209
72225e17
JK
210 if {[test_compiler_info {gcc-[0-3]-*}]
211 || [test_compiler_info {gcc-4-[0-5]-*}]} {
212 setup_xfail "gcc/46955" *-*-*
213 }
326fd672
TT
214 gdb_test "python print ttype.template_argument(2)" "&C::c"
215}
361ae042 216
bfd31e71 217# Perform C Tests.
db8e4570
UW
218build_inferior "${binfile}" "c"
219restart_gdb "${binfile}"
7d1bf85c 220
f6bbabf0
PM
221# Skip all tests if Python scripting is not enabled.
222if { [skip_python_tests] } { continue }
7d1bf85c 223
f6bbabf0 224runto_bp "break to inspect struct and array."
bfd31e71 225test_fields "c"
7a81bdbf 226test_enums
bfd31e71
PM
227
228# Perform C++ Tests.
db8e4570
UW
229build_inferior "${binfile}-cxx" "c++"
230restart_gdb "${binfile}-cxx"
f6bbabf0 231runto_bp "break to inspect struct and array."
bfd31e71
PM
232test_fields "c++"
233test_base_class
361ae042 234test_range
326fd672 235test_template
7a81bdbf 236test_enums
This page took 0.363408 seconds and 4 git commands to generate.