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