]>
Commit | Line | Data |
---|---|---|
1d506c26 | 1 | # Copyright 2022-2024 Free Software Foundation, Inc. |
01772c54 PA |
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 | ||
71630188 TV |
16 | # Format hex value VAL for language LANG. |
17 | ||
18 | proc hex_for_lang { lang val } { | |
1b4633f8 TV |
19 | set neg_p [regexp ^- $val] |
20 | set val [regsub ^-?0x $val ""] | |
71630188 TV |
21 | if { $lang == "modula-2" } { |
22 | set val 0[string toupper $val]H | |
23 | } else { | |
24 | set val 0x$val | |
25 | } | |
1b4633f8 TV |
26 | if { $neg_p } { |
27 | return -$val | |
28 | } else { | |
29 | return $val | |
30 | } | |
31 | } | |
32 | ||
33 | # Determine whether N fits in type with TYPE_BITS and TYPE_SIGNEDNESS. | |
34 | ||
35 | proc fits_in_type { n type_bits type_signedness } { | |
36 | if { $type_signedness == "s" } { | |
37 | set type_signed_p 1 | |
38 | } elseif { $type_signedness == "u" } { | |
39 | set type_signed_p 0 | |
40 | } else { | |
41 | error "unreachable" | |
42 | } | |
43 | ||
44 | if { $n < 0 && !$type_signed_p } { | |
45 | # Can't fit a negative number in an unsigned type. | |
46 | return 0 | |
47 | } | |
48 | ||
49 | if { $n < 0} { | |
50 | set n_sign -1 | |
51 | set n [expr -$n] | |
52 | } else { | |
53 | set n_sign 1 | |
54 | } | |
55 | ||
56 | set smax [expr 1 << ($type_bits - 1)]; | |
57 | if { $n_sign == -1 } { | |
58 | # Negative number, signed type. | |
59 | return [expr ($n <= $smax)] | |
60 | } elseif { $n_sign == 1 && $type_signed_p } { | |
61 | # Positive number, signed type. | |
62 | return [expr ($n < $smax)] | |
63 | } elseif { $n_sign == 1 && !$type_signed_p } { | |
64 | # Positive number, unsigned type. | |
65 | return [expr ($n >> $type_bits) == 0] | |
66 | } else { | |
67 | error "unreachable" | |
68 | } | |
69 | } | |
70 | ||
1d8c0dfa TV |
71 | # Return 1 if LANG is a c-like language, in the sense that it uses the same |
72 | # parser. | |
73 | ||
74 | proc c_like { lang } { | |
75 | set res 0 | |
76 | switch $lang { | |
77 | c | |
78 | - c++ | |
79 | - asm | |
80 | - objective-c | |
81 | - opencl | |
82 | - minimal {set res 1} | |
83 | } | |
84 | return $res | |
85 | } | |
86 | ||
1b4633f8 TV |
87 | # Parse number N for LANG, and return a list of expected type and value. |
88 | ||
89 | proc parse_number { lang n } { | |
90 | global re_overflow | |
91 | ||
92 | set hex_p [regexp ^-?0x $n] | |
93 | ||
94 | global hex decimal | |
95 | if { $hex_p } { | |
96 | set any $hex | |
97 | } else { | |
98 | set any $decimal | |
99 | } | |
100 | ||
101 | global sizeof_long_long sizeof_long sizeof_int | |
102 | set long_long_bits [expr $sizeof_long_long * 8] | |
103 | set long_bits [expr $sizeof_long * 8] | |
104 | set int_bits [expr $sizeof_int * 8] | |
105 | ||
106 | if { $lang == "rust" } { | |
107 | if { [fits_in_type $n 32 s] } { | |
108 | return [list "i32" $n] | |
109 | } elseif { [fits_in_type $n 64 s] } { | |
110 | return [list "i64" $n] | |
d760ae22 TT |
111 | } elseif { [fits_in_type $n 128 u] } { |
112 | return [list "i128" $n] | |
113 | } elseif { [fits_in_type $n 128 u] } { | |
114 | return [list "i128" $n] | |
1b4633f8 TV |
115 | } else { |
116 | # Overflow. | |
1390b65a | 117 | return [list $re_overflow $re_overflow] |
1b4633f8 TV |
118 | } |
119 | } elseif { $lang == "d" } { | |
120 | if { [fits_in_type $n 32 s] } { | |
121 | return [list int $n] | |
122 | } elseif { [fits_in_type $n 32 u] } { | |
123 | if { $hex_p } { | |
124 | return [list uint $n] | |
125 | } else { | |
126 | return [list long $n] | |
127 | } | |
128 | } elseif { [fits_in_type $n 64 s] } { | |
129 | return [list long $n] | |
130 | } elseif { [fits_in_type $n 64 u] } { | |
131 | return [list ulong $n] | |
132 | } else { | |
133 | # Overflow. | |
134 | return [list $re_overflow $re_overflow] | |
135 | } | |
136 | } elseif { $lang == "ada" } { | |
137 | if { [fits_in_type $n $int_bits s] } { | |
138 | return [list "<$sizeof_int-byte integer>" $n] | |
139 | } elseif { [fits_in_type $n $long_bits s] } { | |
140 | return [list "<$sizeof_long-byte integer>" $n] | |
141 | } elseif { [fits_in_type $n $long_bits u] } { | |
142 | return [list "<$sizeof_long-byte integer>" $n] | |
143 | } elseif { [fits_in_type $n $long_long_bits s] } { | |
144 | return [list "<$sizeof_long_long-byte integer>" $n] | |
145 | } elseif { [fits_in_type $n $long_long_bits u] } { | |
146 | # Note: Interprets ULLONG_MAX as -1. | |
147 | return [list "<$sizeof_long_long-byte integer>" $n] | |
8a2ced4f TT |
148 | } elseif { [fits_in_type $n 128 u] } { |
149 | return [list "<16-byte integer>" $n] | |
1b4633f8 TV |
150 | } else { |
151 | # Overflow. | |
ac3afe36 | 152 | return [list $re_overflow $re_overflow] |
1b4633f8 TV |
153 | } |
154 | } elseif { $lang == "modula-2" } { | |
155 | if { [string equal $n -0] } { | |
156 | # Note: 0 is CARDINAL, but -0 is an INTEGER. | |
157 | return [list "INTEGER" 0] | |
158 | } | |
159 | if { $n < 0 && [fits_in_type $n $int_bits s] } { | |
160 | return [list "INTEGER" $n] | |
161 | } elseif { [fits_in_type $n $int_bits u] } { | |
162 | return [list "CARDINAL" $n] | |
163 | } else { | |
164 | # Overflow. | |
999f7adc | 165 | return [list $re_overflow $re_overflow] |
1b4633f8 TV |
166 | } |
167 | } elseif { $lang == "fortran" } { | |
168 | if { [fits_in_type $n $int_bits s] } { | |
169 | return [list int $n] | |
170 | } elseif { [fits_in_type $n $int_bits u] } { | |
171 | return [list "unsigned int" $n] | |
172 | } elseif { [fits_in_type $n $long_bits s] } { | |
173 | return [list long $n] | |
174 | } elseif { [fits_in_type $n $long_bits u] } { | |
175 | return [list "unsigned long" $n] | |
176 | } else { | |
177 | # Overflow. | |
a2c0d041 | 178 | return [list $re_overflow $re_overflow] |
1b4633f8 TV |
179 | } |
180 | } else { | |
1d8c0dfa TV |
181 | if { [c_like $lang] } { |
182 | if { $hex_p } { | |
183 | # C Hex. | |
184 | set have_unsigned 1 | |
185 | } else { | |
186 | # C Decimal. Unsigned not allowed according. | |
187 | if { [fits_in_type $n $long_long_bits s] } { | |
188 | # Fits in largest signed type. | |
189 | set have_unsigned 0 | |
190 | } else { | |
191 | # Doesn't fit in largest signed type, so ill-formed, but | |
192 | # allow unsigned as a convenience, as compilers do (though | |
193 | # with a warning). | |
194 | set have_unsigned 1 | |
195 | } | |
196 | } | |
197 | } else { | |
198 | # Non-C. | |
199 | set have_unsigned 1 | |
200 | } | |
201 | ||
1b4633f8 TV |
202 | if { [fits_in_type $n $int_bits s] } { |
203 | return [list int $n] | |
1d8c0dfa | 204 | } elseif { $have_unsigned && [fits_in_type $n $int_bits u] } { |
1b4633f8 TV |
205 | return [list "unsigned int" $n] |
206 | } elseif { [fits_in_type $n $long_bits s] } { | |
207 | return [list long $n] | |
1d8c0dfa | 208 | } elseif { $have_unsigned && [fits_in_type $n $long_bits u] } { |
1b4633f8 TV |
209 | return [list "unsigned long" $n] |
210 | } elseif { [fits_in_type $n $long_long_bits s] } { | |
211 | return [list "long long" $n] | |
1d8c0dfa | 212 | } elseif { $have_unsigned && [fits_in_type $n $long_long_bits u] } { |
1b4633f8 TV |
213 | return [list "unsigned long long" $n] |
214 | } else { | |
215 | # Overflow. | |
7af9baa9 | 216 | return [list $re_overflow $re_overflow] |
1b4633f8 TV |
217 | } |
218 | } | |
219 | ||
220 | error "unreachable" | |
71630188 TV |
221 | } |
222 | ||
01772c54 PA |
223 | # Test parsing numbers. Several language parsers had the same bug |
224 | # around parsing large 64-bit numbers, hitting undefined behavior, and | |
225 | # thus crashing a GDB built with UBSan. This testcase goes over all | |
226 | # languages exercising printing the max 64-bit number, making sure | |
5b758627 | 227 | # that GDB doesn't crash. ARCH is the architecture to test with. |
01772c54 | 228 | |
5b758627 | 229 | proc test_parse_numbers {arch} { |
1b4633f8 TV |
230 | global full_arch_testing |
231 | global tested_archs | |
232 | global verbose | |
233 | ||
5b758627 PA |
234 | set arch_re [string_to_regexp $arch] |
235 | gdb_test "set architecture $arch" "The target architecture is set to \"$arch_re\"." | |
01772c54 | 236 | |
5b758627 PA |
237 | gdb_test_no_output "set language c" |
238 | ||
239 | # Types have different sizes depending on the architecture. | |
240 | # Figure out type sizes before matching patterns in the upcoming | |
241 | # tests. | |
242 | ||
1b4633f8 | 243 | global sizeof_long_long sizeof_long sizeof_int sizeof_short |
5b758627 PA |
244 | set sizeof_long_long [get_sizeof "long long" -1] |
245 | set sizeof_long [get_sizeof "long" -1] | |
246 | set sizeof_int [get_sizeof "int" -1] | |
1b4633f8 | 247 | set sizeof_short [get_sizeof "short" -1] |
5b758627 | 248 | |
1b4633f8 TV |
249 | if { ! $full_arch_testing } { |
250 | set arch_id \ | |
251 | [list $sizeof_long_long $sizeof_long $sizeof_long $sizeof_int \ | |
252 | $sizeof_short] | |
253 | if { [lsearch $tested_archs $arch_id] == -1 } { | |
254 | lappend tested_archs $arch_id | |
255 | } else { | |
256 | return | |
257 | } | |
5b758627 PA |
258 | } |
259 | ||
260 | foreach_with_prefix lang $::all_languages { | |
a2ff7548 | 261 | if { $lang == "unknown" } { |
cb0d58bf TV |
262 | # Tested outside $supported_archs loop. |
263 | continue | |
264 | } elseif { $lang == "auto" || $lang == "local" } { | |
265 | # Avoid duplicate testing. | |
a2ff7548 TV |
266 | continue |
267 | } | |
268 | ||
01772c54 PA |
269 | gdb_test_no_output "set language $lang" |
270 | ||
1b4633f8 TV |
271 | global re_overflow |
272 | if { $lang == "modula-2" || $lang == "fortran" } { | |
273 | set re_overflow "Overflow on numeric constant\\." | |
274 | } elseif { $lang == "ada" } { | |
275 | set re_overflow "Integer literal out of range" | |
1390b65a TV |
276 | } elseif { $lang == "rust" } { |
277 | set re_overflow "Integer literal is too large" | |
01772c54 | 278 | } else { |
1b4633f8 TV |
279 | set re_overflow "Numeric constant too large\\." |
280 | } | |
281 | ||
282 | set basevals { | |
283 | 0xffffffffffffffff | |
284 | 0x7fffffffffffffff | |
285 | 0xffffffff | |
286 | 0x7fffffff | |
287 | 0xffff | |
288 | 0x7fff | |
289 | 0xff | |
290 | 0x7f | |
291 | 0x0 | |
292 | } | |
293 | ||
294 | if { $lang == "modula-2" } { | |
295 | # Modula-2 is the only language that changes the type of an | |
296 | # integral literal based on whether it's prefixed with "-", | |
297 | # so test both scenarios. | |
298 | set prefixes { "" "-" } | |
299 | } else { | |
300 | # For all the other languages, we'd just be testing the | |
301 | # parsing twice, so just test the basic scenario of no prefix. | |
302 | set prefixes { "" } | |
303 | } | |
304 | ||
305 | foreach_with_prefix prefix $prefixes { | |
306 | foreach baseval $basevals { | |
307 | foreach offset { -2 -1 0 1 2 } { | |
308 | set dec_val [expr $baseval + $offset] | |
309 | set hex_val [format "0x%llx" $dec_val] | |
310 | if { $dec_val < 0 } { | |
311 | continue | |
312 | } | |
313 | ||
314 | set dec_val $prefix$dec_val | |
315 | lassign [parse_number $lang $dec_val] type out | |
316 | if { $verbose >= 1 } { verbose -log "EXPECTED: $out" 2 } | |
317 | if { $prefix == "" } { | |
318 | gdb_test "p/u $dec_val" "$out" | |
319 | } else { | |
320 | gdb_test "p/d $dec_val" "$out" | |
321 | } | |
322 | if { $verbose >= 1 } { verbose -log "EXPECTED: $type" 2 } | |
323 | gdb_test "ptype $dec_val" "$type" | |
324 | ||
325 | if { $prefix == "-" } { | |
326 | # Printing with /x below means negative numbers are | |
327 | # converted to unsigned representation. We could | |
328 | # support this by updating the expected patterns. | |
329 | # Possibly, we could print with /u and /d instead of | |
330 | # /x here as well (which would also require updating | |
331 | # expected patterns). | |
332 | # For now, this doesn't seem worth the trouble, | |
333 | # so skip. | |
334 | continue | |
335 | } | |
336 | ||
337 | set hex_val $prefix$hex_val | |
338 | lassign [parse_number $lang $hex_val] type out | |
339 | set hex_val [hex_for_lang $lang $hex_val] | |
340 | if { $verbose >= 1 } { verbose -log "EXPECTED: $out" 2 } | |
341 | gdb_test "p/x $hex_val" "$out" | |
342 | if { $verbose >= 1 } { verbose -log "EXPECTED: $type" 2 } | |
343 | gdb_test "ptype $hex_val" "$type" | |
5b758627 | 344 | } |
01772c54 PA |
345 | } |
346 | } | |
347 | } | |
348 | } | |
349 | ||
5b758627 PA |
350 | clean_restart |
351 | ||
5b758627 PA |
352 | set supported_archs [get_set_option_choices "set architecture"] |
353 | # There should be at least one more than "auto". | |
354 | gdb_assert {[llength $supported_archs] > 1} "at least one architecture" | |
355 | ||
356 | set all_languages [get_set_option_choices "set language"] | |
357 | ||
8d45c3a8 TV |
358 | gdb_test_no_output "set language unknown" |
359 | gdb_test "p/x 0" \ | |
360 | "expression parsing not implemented for language \"Unknown\"" | |
361 | ||
1b4633f8 TV |
362 | # If 1, test each arch. If 0, test one arch for each sizeof |
363 | # short/int/long/longlong configuration. | |
364 | # For a build with --enable-targets=all, full_arch_testing == 0 takes 15s, | |
365 | # while full_arch_testing == 1 takes 9m20s. | |
366 | set full_arch_testing 0 | |
367 | ||
368 | set tested_archs {} | |
5b758627 PA |
369 | foreach_with_prefix arch $supported_archs { |
370 | if {$arch == "auto"} { | |
cb0d58bf | 371 | # Avoid duplicate testing. |
5b758627 PA |
372 | continue |
373 | } | |
374 | test_parse_numbers $arch | |
375 | } |