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