]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.python/py-type.exp
8b378d46e4b3fbde34ec7bf800c1b0ff715836c1
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-type.exp
1 # Copyright (C) 2009, 2010 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 if $tracelevel then {
20 strace $tracelevel
21 }
22
23 load_lib gdb-python.exp
24
25 set testfile "py-type"
26 set srcfile ${testfile}.c
27 set binfile ${objdir}/${subdir}/${testfile}
28
29 if [get_compiler_info not-used c++] {
30 return -1;
31 }
32
33 # Build inferior to language specification.
34 proc build_inferior {lang} {
35 global srcdir subdir srcfile binfile testfile hex
36
37 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "debug $lang"] != "" } {
38 untested "Couldn't compile ${srcfile} in $lang mode"
39 return -1
40 }
41 }
42
43 # Restart GDB.
44 proc restart_gdb {} {
45 global srcdir subdir srcfile binfile testfile hex
46
47 gdb_exit
48 gdb_start
49 gdb_reinitialize_dir $srcdir/$subdir
50 gdb_load ${binfile}
51
52 if ![runto_main ] then {
53 perror "couldn't run to breakpoint"
54 return
55 }
56 }
57
58 # Set breakpoint and run to that breakpoint.
59 proc runto_bp {bp} {
60 gdb_breakpoint [gdb_get_line_number $bp]
61 gdb_continue_to_breakpoint $bp
62 }
63
64 proc test_fields {lang} {
65 global gdb_prompt
66
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"
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"
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
89 # Test regression PR python/10805
90 gdb_py_test_silent_cmd "print ar" "print value" 1
91 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
92 gdb_test "python fields = ar.type.fields()"
93 gdb_test "python print len(fields)" "1" "Check the number of fields"
94 gdb_test "python print fields\[0\].type" "<range type>" "Check array field type"
95
96 gdb_test "python print ar\[0\].cast(ar\[0\].type.array(1))" \
97 ".1, 2." "cast to array with one argument"
98 gdb_test "python print ar\[0\].cast(ar\[0\].type.array(0, 1))" \
99 ".1, 2." "cast to array with two arguments"
100
101 gdb_test "python print ar\[0\].type == ar\[0\].type" "True"
102 }
103
104 proc test_base_class {} {
105 gdb_py_test_silent_cmd "print d" "print value" 1
106 gdb_py_test_silent_cmd "python d = gdb.history (0)" "get value from history" 1
107 gdb_py_test_silent_cmd "python fields = d.type.fields()" "get value from history" 1
108 gdb_test "python print len(fields)" "3" "Check the number of fields"
109 gdb_test "python print fields\[0\].is_base_class" "True" "Check base class"
110 gdb_test "python print fields\[1\].is_base_class" "False" "Check base class"
111 }
112
113 proc test_range {} {
114
115 # Test a valid range request.
116 gdb_py_test_silent_cmd "print ar" "print value" 1
117 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
118 gdb_test "python print len(ar.type.range())" "2" "Check correct tuple length"
119 gdb_test "python print ar.type.range()\[0\]" "0" "Check low range"
120 gdb_test "python print ar.type.range()\[1\]" "1" "Check high range"
121
122 # Test a range request on a ranged type.
123 gdb_py_test_silent_cmd "print ar" "print value" 1
124 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
125 gdb_py_test_silent_cmd "python fields = ar.type.fields()" "get fields" 1
126 gdb_test "python print fields\[0\].type.range()\[0\]" "0" "Check range type low bound"
127 gdb_test "python print fields\[0\].type.range()\[1\]" "1" "Check range type high bound"
128
129 # Test where a range does not exist.
130 gdb_py_test_silent_cmd "print st" "print value" 1
131 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1
132 gdb_test "python print st.type.range()" "RuntimeError: This type does not have a range.*" "Check range for non ranged type."
133 }
134
135 # Some tests of template arguments.
136 proc test_template {} {
137 gdb_py_test_silent_cmd \
138 "python ttype = gdb.parse_and_eval('temvar').type" \
139 "get type of temvar" \
140 1
141
142 gdb_test "python print ttype.template_argument(0)" "D"
143 gdb_test "python print isinstance(ttype.template_argument(0), gdb.Type)" \
144 "True"
145
146 # The next two tests require a GCC that emits DW_TAG_template_*.
147 # GCC 4.4 does not emit it, 4.5 and 6 do emit it.
148 set have_older_gcc 0
149 if {[test_compiler_info {gcc-[0-3]-*}]
150 || [test_compiler_info {gcc-4-[0-4]-*}]} {
151 set have_older_gcc 1
152 }
153 if $have_older_gcc { setup_xfail *-*-* }
154 gdb_test "python print ttype.template_argument(1)" "23"
155 if $have_older_gcc { setup_xfail *-*-* }
156 gdb_test "python print isinstance(ttype.template_argument(1), gdb.Value)" \
157 "True"
158
159 setup_kfail "gcc/41736" *-*-*
160 gdb_test "python print ttype.template_argument(2)" "&C::c"
161 }
162
163 # Perform C Tests.
164 build_inferior "c"
165 restart_gdb
166
167 # Skip all tests if Python scripting is not enabled.
168 if { [skip_python_tests] } { continue }
169
170 runto_bp "break to inspect struct and array."
171 test_fields "c"
172
173 # Perform C++ Tests.
174 build_inferior "c++"
175 restart_gdb
176 runto_bp "break to inspect struct and array."
177 test_fields "c++"
178 test_base_class
179 test_range
180 test_template