1a46317780062415ffd187cd3f725debe8687479
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.rust / simple.exp
1 # Copyright (C) 2016-2017 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 # Test expression parsing and evaluation that requires Rust compiler.
17
18 load_lib rust-support.exp
19 if {[skip_rust_tests]} {
20 continue
21 }
22
23 standard_testfile .rs
24 if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug rust}]} {
25 return -1
26 }
27
28 set line [gdb_get_line_number "set breakpoint here"]
29 if {![runto ${srcfile}:$line]} {
30 untested "could not run to breakpoint"
31 return -1
32 }
33
34 gdb_test "print a" " = \\(\\)"
35 gdb_test "ptype a" " = \\(\\)"
36 gdb_test "print sizeof(a)" " = 0"
37
38 gdb_test "print b" " = \\\[\\\]"
39 gdb_test "ptype b" " = \\\[i32; 0\\\]"
40 gdb_test "print *(&b as *const \[i32; 0\])" " = \\\[\\\]"
41 gdb_test "print *(&b as *const \[i32; 0_0\])" " = \\\[\\\]"
42
43 gdb_test "print c" " = 99"
44 gdb_test "ptype c" " = i32"
45 gdb_test "print sizeof(c)" " = 4"
46
47 gdb_test "print c = 87" " = \\(\\)"
48 gdb_test "print c" " = 87"
49 gdb_test "print c += 3" " = \\(\\)"
50 gdb_test "print c" " = 90"
51 gdb_test "print c -= 90" " = \\(\\)"
52 gdb_test "print c" " = 0"
53 gdb_test "print *&c" " = 0"
54 gdb_test "print *(&c as &i32)" " = 0"
55 gdb_test "print *(&c as *const i32)" " = 0"
56 gdb_test "print *(&c as *mut i32)" " = 0"
57
58 gdb_test "print/c f\[0\]" " = 104 'h'"
59
60 gdb_test "print j" " = simple::Unit"
61 gdb_test "ptype j" " = struct simple::Unit"
62 gdb_test "print j2" " = simple::Unit"
63 gdb_test "ptype j2" " = struct simple::Unit"
64 gdb_test "print simple::Unit" " = simple::Unit"
65 gdb_test "print simple::Unit{}" " = simple::Unit"
66
67 gdb_test "print g" " = \\(u8 \\(\\*\\)\\\[6\\\]\\) $hex b\"hi bob\""
68 gdb_test "ptype g" " = u8 \\(\\*\\)\\\[6\\\]"
69
70 gdb_test "print v" " = simple::Something::Three"
71 gdb_test_sequence "ptype v" "" {
72 " = enum simple::Something \\{"
73 " One,"
74 " Two,"
75 " Three,"
76 "\\}"
77 }
78
79 gdb_test "print w" " = \\\[1, 2, 3, 4\\\]"
80 gdb_test "ptype w" " = \\\[i32; 4\\\]"
81 gdb_test "print w\[2\]" " = 3"
82 gdb_test "print w\[2\] @ 2" " = \\\[3, 4\\\]"
83 gdb_test "print w_ptr\[2\]" " = 3"
84 gdb_test "print fromslice" " = 3"
85 gdb_test "print slice\[0\]" " = 3"
86 gdb_test "print slice as &\[i32\]\[0\]" " = 3"
87
88 gdb_test "print x" " = \\(23, 25\\.5\\)"
89 gdb_test "ptype x" " = \\(i32, f64\\)"
90 gdb_test "print x as (i32,f64)" " = \\(23, 25\\.5\\)"
91
92 gdb_test "print y" " = simple::HiBob \\{field1: 7, field2: 8\\}"
93 gdb_test_sequence "ptype y" "" {
94 " = struct simple::HiBob \\{"
95 " field1: i32,"
96 " field2: u64,"
97 "\\}"
98 }
99 gdb_test "print y.field2" " = 8"
100
101 gdb_test "print z" " = simple::ByeBob \\(7, 8\\)"
102 gdb_test_sequence "ptype z" "" {
103 " = struct simple::ByeBob \\("
104 " i32,"
105 " u64,"
106 "\\)"
107 }
108 gdb_test "print z.1" " = 8"
109
110 gdb_test "print univariant" " = simple::Univariant::Foo{a: 1}"
111 gdb_test "print univariant.a" " = 1"
112 gdb_test "print univariant_anon" " = simple::UnivariantAnon::Foo\\(1\\)"
113 gdb_test "print univariant_anon.0" " = 1"
114
115 gdb_test_sequence "ptype simple::Univariant" "" {
116 "type = enum simple::Univariant \\{"
117 " Foo\\{a: u8\\},"
118 "\\}"
119 }
120
121 gdb_test_sequence "ptype simple::UnivariantAnon" "" {
122 "type = enum simple::UnivariantAnon \\{"
123 " Foo\\(u8\\),"
124 "\\}"
125 }
126
127 gdb_test_sequence "ptype simple::ByeBob" "" {
128 " = struct simple::ByeBob \\("
129 " i32,"
130 " u64,"
131 "\\)"
132 }
133 gdb_test "print simple::ByeBob(0xff, 5)" \
134 " = simple::ByeBob \\(255, 5\\)"
135 gdb_test "print simple::ByeBob\{field1: 0xff, field2:5\}" \
136 "Struct expression applied to non-struct type"
137
138 gdb_test "print simple::HiBob(0xff, 5)" \
139 "Type simple::HiBob is not a tuple struct"
140 gdb_test "print sizeof(simple::HiBob)" " = \[0-9\]+"
141 gdb_test "print simple::HiBob + 5" \
142 "Found type 'simple::HiBob', which can't be evaluated in this context"
143 gdb_test "print nosuchsymbol" \
144 "No symbol 'nosuchsymbol' in current context"
145
146 gdb_test "print e" " = simple::MoreComplicated::Two\\(73\\)"
147 gdb_test "print e2" \
148 " = simple::MoreComplicated::Four\\{this: true, is: 8, a: 109 'm', struct_: 100, variant: 10\\}"
149 gdb_test "print sizeof(e)" " = 24"
150 gdb_test_sequence "ptype e" "" {
151 " = enum simple::MoreComplicated \\{"
152 " One,"
153 " Two\\(i32\\),"
154 " Three\\(simple::HiBob\\),"
155 " Four\\{this: bool, is: u8, a: char, struct_: u64, variant: u32\\},"
156 "\\}"
157 }
158
159 gdb_test "print e.0" " = 73"
160 gdb_test "print e.1" \
161 "Cannot access field 1 of variant simple::MoreComplicated::Two, there are only 1 fields"
162 gdb_test "print e.foo" \
163 "Attempting to access named field foo of tuple variant simple::MoreComplicated::Two, which has only anonymous fields"
164
165 gdb_test "print e2.variant" " = 10"
166 gdb_test "print e2.notexist" \
167 "Could not find field notexist of struct variant simple::MoreComplicated::Four"
168 gdb_test "print e2.0" \
169 "Variant simple::MoreComplicated::Four is not a tuple variant"
170
171 gdb_test "print k" " = simple::SpaceSaver::Nothing"
172 gdb_test "print l" " = simple::SpaceSaver::Thebox\\(9, $hex\\)"
173 gdb_test "print *l.1" " = 1729"
174
175 gdb_test "print diff2(3, 7)" " = -4"
176 gdb_test "print self::diff2(8, 9)" " = -1"
177 gdb_test "print ::diff2(23, -23)" " = 46"
178
179 gdb_test "ptype diff2" "fn \\(i32, i32\\) -> i32"
180 gdb_test "ptype empty" "fn \\(\\)"
181
182 gdb_test "print (diff2 as fn(i32, i32) -> i32)(19, -2)" " = 21"
183
184 # We need the ".*" because currently we don't extract the length and
185 # use it to intelligently print the string data.
186 gdb_test "print \"hello rust\"" \
187 " = &str \\{data_ptr: $hex \"hello rust.*\", length: 10\\}"
188 gdb_test "print \"hello" "Unexpected EOF in string"
189 gdb_test "print r##\"hello \" rust\"##" \
190 " = &str \\{data_ptr: $hex \"hello \\\\\" rust.*\", length: 12\\}"
191 gdb_test "print r\"hello" "Unexpected EOF in string"
192 gdb_test "print r###\"###hello\"" "Unexpected EOF in string"
193 gdb_test "print r###\"###hello\"##" "Unexpected EOF in string"
194 gdb_test "print r###\"hello###" "Unexpected EOF in string"
195
196 gdb_test "print 0..5" " = .*::ops::Range.* \\{start: 0, end: 5\\}"
197 gdb_test "print ..5" " = .*::ops::RangeTo.* \\{end: 5\\}"
198 gdb_test "print 5.." " = .*::ops::RangeFrom.* \\{start: 5\\}"
199 gdb_test "print .." " = .*::ops::RangeFull"
200
201 gdb_test "print str_some" \
202 " = core::option::Option<\[a-z\]+::string::String>::Some\\(\[a-z\]+::string::String .*"
203 gdb_test "print str_none" " = core::option::Option<\[a-z\]+::string::String>::None"
204 gdb_test "print int_some" " = core::option::Option::Some\\(1\\)"
205 gdb_test "print int_none" " = core::option::Option::None"
206 gdb_test "print box_some" " = core::option::Option<\[a-z:\]*Box<u8>>::Some\\(.*\\)"
207 gdb_test "print box_none" " = core::option::Option<\[a-z:\]*Box<u8>>::None"
208 gdb_test "print custom_some" \
209 " = simple::NonZeroOptimized::Value\\(\[a-z\]+::string::String .*"
210 gdb_test "print custom_none" " = simple::NonZeroOptimized::Empty"
211
212 proc test_one_slice {svar length base range} {
213 global hex
214
215 set result " = &\\\[.*\\\] \\{data_ptr: $hex, length: $length\\}"
216
217 gdb_test "print $svar" $result
218 gdb_test "print &${base}\[${range}\]" $result
219 }
220
221 test_one_slice slice 1 w 2..3
222 test_one_slice slice2 1 slice 0..1
223
224 test_one_slice all1 4 w ..
225 test_one_slice all2 1 slice ..
226
227 test_one_slice from1 3 w 1..
228 test_one_slice from2 0 slice 1..
229
230 test_one_slice to1 3 w ..3
231 test_one_slice to2 1 slice ..1
232
233 gdb_test "print w\[2..3\]" "Can't take slice of array without '&'"
234
235
236 gdb_test_sequence "complete print y.f" "" \
237 {"print y.field1" "print y.field2"}
238 gdb_test_sequence "complete print y." "" \
239 {"print y.field1" "print y.field2"}
240
241 # Unimplemented, but we can at least test the parser productions.
242 gdb_test "print (1,2,3)" "Tuple expressions not supported yet"
243 gdb_test "print (1,)" "Tuple expressions not supported yet"
244 gdb_test "print (1)" " = 1"
245
246 gdb_test "print 23..97.0" "Range expression with different types"
247
248 gdb_test "print (*parametrized.next.val)" \
249 " = simple::ParametrizedStruct<i32> {next: simple::ParametrizedEnum<\[a-z:\]*Box<simple::ParametrizedStruct<i32>>>::Empty, value: 1}"
250 gdb_test "print parametrized.next.val" \
251 " = \\(simple::ParametrizedStruct<i32> \\*\\) $hex"
252 gdb_test "print parametrized" \
253 " = simple::ParametrizedStruct<i32> \\{next: simple::ParametrizedEnum<\[a-z:\]*Box<simple::ParametrizedStruct<i32>>>::Val\\{val: $hex\\}, value: 0\\}"
254
255
256 load_lib gdb-python.exp
257 if {[skip_python_tests]} {
258 continue
259 }
260
261 gdb_test "python print(gdb.lookup_type('simple::HiBob'))" "simple::HiBob"
This page took 0.049004 seconds and 3 git commands to generate.