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