]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.python/py-format-string.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-format-string.exp
CommitLineData
1d506c26 1# Copyright (C) 2009-2024 Free Software Foundation, Inc.
52093e1b
MB
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
17# gdb.Value.format_string () method.
18
19load_lib gdb-python.exp
20
d82e5429 21require allow_python_tests
79749205 22
52093e1b
MB
23standard_testfile
24
05e682e3
SL
25gdb_exit
26gdb_start
05e682e3 27
52093e1b
MB
28# Build inferior to language specification.
29proc build_inferior {exefile lang} {
30 global srcdir subdir srcfile testfile hex
31
b19f4f61
TV
32 set flags [list debug $lang]
33 if { $lang == "c" } {
34 lappend flags additional_flags=-std=c99
35 }
36
37 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" \
38 executable $flags] != "" } {
52093e1b
MB
39 untested "failed to compile in $lang mode"
40 return -1
41 }
42
43 return 0
44}
45
46# Restart GDB.
47proc prepare_gdb {exefile} {
48 global srcdir subdir srcfile testfile hex
49
9b2234b0 50 clean_restart $exefile
52093e1b 51
b0e16ca5 52 if {![runto_main]} {
52093e1b
MB
53 return
54 }
55
56 # Load the pretty printer.
57 set remote_python_file \
58 [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]
59 gdb_test_no_output "source ${remote_python_file}" "load python file"
60
61 runto_bp "break here"
62}
63
64# Set breakpoint and run to that breakpoint.
65proc runto_bp {bp} {
66 gdb_breakpoint [gdb_get_line_number $bp]
67 gdb_continue_to_breakpoint $bp
68}
69
70# Set an option using the GDB command in $set_cmd, execute $body, and then
71# restore the option using the GDB command in $unset_cmd.
72proc with_temp_option { set_cmd unset_cmd body } {
73 with_test_prefix $set_cmd {
74 gdb_test "$set_cmd" ".*"
75 uplevel 1 $body
76 gdb_test "$unset_cmd" ".*"
77 }
78}
79
80# A regular expression for a pointer.
81set default_pointer_regexp "0x\[a-fA-F0-9\]+"
82
83# A regular expression for a non-expanded C++ reference.
84#
85# Stringifying a C++ reference produces an address preceeded by a "@" in
86# Python, but, by default, the C++ reference/class is expanded by the
87# GDB print command.
88set default_ref_regexp "@${default_pointer_regexp}"
89
90# The whole content of the C variable a_big_string, i.e. the whole English
91# alphabet repeated 10 times.
92set whole_big_string ""
93set alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
94for {set i 0} {$i < 10} {incr i} {
95 append whole_big_string $alphabet
96}
97unset alphabet
98
99# Produces a potentially cut down version of $whole_big_string like GDB
100# would represent it.
101# $max is the maximum number of characters allowed in the string (but
102# the return value may contain more to accound for the extra quotes and
103# "..." added by GDB).
104proc get_cut_big_string { max } {
105 global whole_big_string
106
107 set whole_size [string length $whole_big_string]
108 if { $max > $whole_size } {
109 return "\"${whole_big_string}\""
110 }
111
112 set cut_string [string range $whole_big_string 0 [expr $max - 1]]
113 return "\"${cut_string}\"..."
114}
115
116# A dictionary mapping from C variable names to their default string
117# representation when using str () or gdb.Value.format_string () with
118# no arguments.
119# This usually matches what the print command prints if used with no
120# options, except for C++ references which are not expanded by
121# default in Python. See the comment above $default_ref_regexp.
122set default_regexp_dict [dict create \
123 "a_point_t" "Pretty Point \\(42, 12\\)" \
124 "a_point_t_pointer" $default_pointer_regexp \
125 "a_point_t_ref" "Pretty Point \\(42, 12\\)" \
126 "another_point" "Pretty Point \\(123, 456\\)" \
ecf25064 127 "a_struct_with_point" "\\{the_point = Pretty Point \\(42, 12\\)\\}" \
9ef62df0 128 "a_struct_with_union" "\\{the_union = \\{an_int = 707406378, a_char = 42 '\\*'\\}\\}" \
52093e1b
MB
129 "an_enum" "ENUM_BAR" \
130 "a_string" "${default_pointer_regexp} \"hello world\"" \
131 "a_binary_string" "${default_pointer_regexp} \"hello\"" \
132 "a_binary_string_array" "\"hello\\\\000world\"" \
133 "a_big_string" [get_cut_big_string 200] \
134 "an_array" "\\{2, 3, 5\\}" \
135 "an_array_with_repetition" "\\{1, 3 <repeats 12 times>, 5, 5, 5\\}" \
136 "a_symbol_pointer" "${default_pointer_regexp} <global_symbol>" \
137 "a_base_ref" "${default_ref_regexp}" \
138 ]
139
140# A sentinel value to pass to function to get them to use a default value
141# instead.
142# Note that we cannot use $undefined for default arguments in function
143# definitions as we would just get the literal "$undefined" string, so
144# we need to repeat the string.
145set undefined "\000UNDEFINED\000"
146
147# Return $value if it's not $undefined, otherwise return the default value
148# (from $default_regexp_dict) for the variable $var.
149proc get_value_or_default { var value } {
150 global undefined
151 if { $value != $undefined } {
152 return $value
153 }
154
155 global default_regexp_dict
156 return [dict get $default_regexp_dict $var]
157}
158
159# Check that using gdb.Value.format_string on the value representing the
160# variable $var produces $expected.
161proc check_format_string {
162 var
163 opts
164 { expected "\000UNDEFINED\000" }
165 { name "\000UNDEFINED\000" }
166 } {
167 global undefined
168
169 set expected [get_value_or_default $var $expected]
170 if { $name == $undefined } {
171 set name "${var} with option ${opts}"
172 }
173
174 gdb_test \
175 "python print (gdb.parse_and_eval ('${var}').format_string (${opts}))" \
176 $expected \
177 $name
178}
179
180# Check that printing $var with no options set, produces the expected
181# output.
182proc check_var_with_no_opts {
183 var
184 { expected "\000UNDEFINED\000" }
185 } {
186 set expected [get_value_or_default $var $expected]
187
188 with_test_prefix "${var}" {
189 check_format_string \
190 $var \
191 "" \
192 $expected \
193 "no opts"
194 # str () should behave like gdb.Value.format_string () with no args.
195 gdb_test \
196 "python print (str (gdb.parse_and_eval ('${var}')))" \
197 $expected \
198 "str"
199 }
200}
201
202# Check that printing $var with $opt set to True and set to False,
203# produces the expected output.
204proc check_var_with_bool_opt {
205 opt
206 var
207 { true_expected "\000UNDEFINED\000" }
208 { false_expected "\000UNDEFINED\000" }
209 } {
210 set true_expected [get_value_or_default $var $true_expected]
211 set false_expected [get_value_or_default $var $false_expected]
212
213 with_test_prefix "${var} with option ${opt}" {
214 # Option set to True.
215 check_format_string \
216 $var \
217 "${opt}=True" \
218 $true_expected \
219 "${opt}=true"
220 # Option set to False.
221 check_format_string \
222 $var \
223 "${opt}=False" \
224 $false_expected \
225 "${opt}=false"
226 }
227}
228
229# Test gdb.Value.format_string with no options.
5fc5a1b8 230proc_with_prefix test_no_opts {} {
52093e1b
MB
231 global current_lang
232
233 check_var_with_no_opts "a_point_t"
234 check_var_with_no_opts "a_point_t_pointer"
235 check_var_with_no_opts "another_point"
236 check_var_with_no_opts "a_struct_with_union"
237 check_var_with_no_opts "an_enum"
238 check_var_with_no_opts "a_string"
239 check_var_with_no_opts "a_binary_string"
240 check_var_with_no_opts "a_binary_string_array"
241 check_var_with_no_opts "a_big_string"
242 check_var_with_no_opts "an_array"
243 check_var_with_no_opts "an_array_with_repetition"
244 check_var_with_no_opts "a_symbol_pointer"
245
246 if { $current_lang == "c++" } {
247 # Nothing changes in all of the C++ tests because deref_refs is not
248 # True.
249 check_var_with_no_opts "a_point_t_ref"
250 check_var_with_no_opts "a_base_ref"
251 }
252}
253
254# Test the raw option for gdb.Value.format_string.
5fc5a1b8 255proc_with_prefix test_raw {} {
52093e1b
MB
256 global current_lang
257 global default_ref_regexp
258
259 check_var_with_bool_opt "raw" "a_point_t" \
260 "{x = 42, y = 12}"
261 check_var_with_bool_opt "raw" "a_point_t_pointer"
262 check_var_with_bool_opt "raw" "another_point" \
263 "{x = 123, y = 456}"
264 check_var_with_bool_opt "raw" "a_struct_with_union"
265 check_var_with_bool_opt "raw" "an_enum"
266 check_var_with_bool_opt "raw" "a_string"
267 check_var_with_bool_opt "raw" "a_binary_string"
268 check_var_with_bool_opt "raw" "a_binary_string_array"
269 check_var_with_bool_opt "raw" "a_big_string"
270 check_var_with_bool_opt "raw" "an_array"
271 check_var_with_bool_opt "raw" "an_array_with_repetition"
272 check_var_with_bool_opt "raw" "a_symbol_pointer"
273
274 if { $current_lang == "c++" } {
275 check_var_with_bool_opt "raw" "a_point_t_ref" \
276 ${default_ref_regexp}
277 check_var_with_bool_opt "raw" "a_base_ref"
278 }
279
280 with_temp_option \
281 "disable pretty-printer '' test_lookup_function" \
282 "enable pretty-printer '' test_lookup_function" {
283 check_var_with_no_opts "a_point_t" \
284 "{x = 42, y = 12}"
285 check_var_with_bool_opt "raw" "a_point_t" \
286 "{x = 42, y = 12}" \
287 "{x = 42, y = 12}"
288 }
289}
290
291# Test the pretty_arrays option for gdb.Value.format_string.
5fc5a1b8 292proc_with_prefix test_pretty_arrays {} {
52093e1b
MB
293 global current_lang
294
d5cf82c0 295 set an_array_pretty "\\{\[\r\n\]+ 2,\[\r\n\]+ 3,\[\r\n\]+ 5\[\r\n\]+\\}"
52093e1b 296 set an_array_with_repetition_pretty \
d5cf82c0 297 "\\{\[\r\n\]+ 1,\[\r\n\]+ 3 <repeats 12 times>,\[\r\n\]+ 5,\[\r\n\]+ 5,\[\r\n\]+ 5\[\r\n\]+\\}"
52093e1b
MB
298
299 check_var_with_bool_opt "pretty_arrays" "a_point_t"
300 check_var_with_bool_opt "pretty_arrays" "a_point_t_pointer"
301 check_var_with_bool_opt "pretty_arrays" "another_point"
302 check_var_with_bool_opt "pretty_arrays" "a_struct_with_union"
303 check_var_with_bool_opt "pretty_arrays" "an_enum"
304 check_var_with_bool_opt "pretty_arrays" "a_string"
305 check_var_with_bool_opt "pretty_arrays" "a_binary_string"
306 check_var_with_bool_opt "pretty_arrays" "a_binary_string_array"
307 check_var_with_bool_opt "pretty_arrays" "a_big_string"
308 check_var_with_bool_opt "pretty_arrays" "an_array" \
309 $an_array_pretty
310 check_var_with_bool_opt "pretty_arrays" "an_array_with_repetition" \
311 $an_array_with_repetition_pretty
312 check_var_with_bool_opt "pretty_arrays" "a_symbol_pointer"
313
314 if { $current_lang == "c++" } {
315 check_var_with_bool_opt "pretty_arrays" "a_point_t_ref"
316 check_var_with_bool_opt "pretty_arrays" "a_base_ref"
317 }
318
319 with_temp_option "set print array on" "set print array off" {
320 check_var_with_no_opts "an_array" \
321 $an_array_pretty
322 check_var_with_bool_opt "pretty_arrays" "an_array" \
323 $an_array_pretty
324
325 check_var_with_no_opts "an_array_with_repetition" \
326 $an_array_with_repetition_pretty
327 check_var_with_bool_opt "pretty_arrays" "an_array_with_repetition" \
328 $an_array_with_repetition_pretty
329 }
330}
331
332# Test the pretty_structs option for gdb.Value.format_string.
5fc5a1b8 333proc_with_prefix test_pretty_structs {} {
52093e1b
MB
334 global current_lang
335
336 set a_struct_with_union_pretty \
9ef62df0 337 "\\{\[\r\n\]+ the_union = \\{\[\r\n\]+ an_int = 707406378,\[\r\n\]+ a_char = 42 '\\*'\[\r\n\]+ \\}\[\r\n\]+\\}"
52093e1b
MB
338
339 check_var_with_bool_opt "pretty_structs" "a_point_t"
340 check_var_with_bool_opt "pretty_structs" "a_point_t_pointer"
341 check_var_with_bool_opt "pretty_structs" "another_point"
342 check_var_with_bool_opt "pretty_structs" "a_struct_with_union" \
343 $a_struct_with_union_pretty
344 check_var_with_bool_opt "pretty_structs" "an_enum"
345 check_var_with_bool_opt "pretty_structs" "a_string"
346 check_var_with_bool_opt "pretty_structs" "a_binary_string"
347 check_var_with_bool_opt "pretty_structs" "a_binary_string_array"
348 check_var_with_bool_opt "pretty_structs" "a_big_string"
349 check_var_with_bool_opt "pretty_structs" "an_array"
350 check_var_with_bool_opt "pretty_structs" "an_array_with_repetition"
351 check_var_with_bool_opt "pretty_structs" "a_symbol_pointer"
352
353 if { $current_lang == "c++" } {
354 check_var_with_bool_opt "pretty_structs" "a_point_t_ref"
355 check_var_with_bool_opt "pretty_structs" "a_base_ref"
356 }
357
358 with_temp_option "set print structs on" "set print structs off" {
359 check_var_with_no_opts "a_struct_with_union"
360 check_var_with_bool_opt "pretty_structs" "a_struct_with_union" \
361 $a_struct_with_union_pretty
362 }
363
364 # point_t is usually printed through the pretty printer.
365 # Try disabling it.
366 with_temp_option \
367 "disable pretty-printer '' test_lookup_function" \
368 "enable pretty-printer '' test_lookup_function" {
369 check_var_with_no_opts "a_point_t" \
370 "{x = 42, y = 12}"
371 check_var_with_bool_opt "pretty_structs" "a_point_t" \
372 "\\{\[\r\n\]+ x = 42, *\[\r\n\]+ y = 12\[\r\n\]+\\}" \
373 "{x = 42, y = 12}" \
374 }
375}
376
377# Test the array_indexes option for gdb.Value.format_string.
5fc5a1b8 378proc_with_prefix test_array_indexes {} {
52093e1b
MB
379 global current_lang
380
381 set an_array_with_indexes "\\{\\\[0\\\] = 2, \\\[1\\\] = 3, \\\[2\\\] = 5\\}"
382 set an_array_with_repetition_with_indexes \
383 "\\{\\\[0\\\] = 1, \\\[1\\\] = 3 <repeats 12 times>, \\\[13\\\] = 5, \\\[14\\\] = 5, \\\[15\\\] = 5\\}"
384
385 check_var_with_bool_opt "array_indexes" "a_point_t"
386 check_var_with_bool_opt "array_indexes" "a_point_t_pointer"
387 check_var_with_bool_opt "array_indexes" "another_point"
388 check_var_with_bool_opt "array_indexes" "a_struct_with_union"
389 check_var_with_bool_opt "array_indexes" "an_enum"
390 check_var_with_bool_opt "array_indexes" "a_string"
391 check_var_with_bool_opt "array_indexes" "a_binary_string"
392 check_var_with_bool_opt "array_indexes" "a_binary_string_array"
393 check_var_with_bool_opt "array_indexes" "a_big_string"
394 check_var_with_bool_opt "array_indexes" "an_array" \
395 $an_array_with_indexes
396 check_var_with_bool_opt "array_indexes" "an_array_with_repetition" \
397 $an_array_with_repetition_with_indexes
398 check_var_with_bool_opt "array_indexes" "a_symbol_pointer"
399
400 if { $current_lang == "c++" } {
401 check_var_with_bool_opt "array_indexes" "a_point_t_ref"
402 check_var_with_bool_opt "array_indexes" "a_base_ref"
403 }
404
405 with_temp_option \
406 "set print array-indexes on" \
407 "set print array-indexes off" {
408 check_var_with_no_opts "an_array" \
409 $an_array_with_indexes
410 check_var_with_bool_opt "array_indexes" "an_array" \
411 $an_array_with_indexes
412
413 check_var_with_no_opts "an_array_with_repetition" \
414 $an_array_with_repetition_with_indexes
415 check_var_with_bool_opt "array_indexes" "an_array_with_repetition" \
416 $an_array_with_repetition_with_indexes
417 }
418}
419
420# Test the symbols option for gdb.Value.format_string.
5fc5a1b8 421proc_with_prefix test_symbols {} {
52093e1b
MB
422 global undefined
423 global current_lang
424 global default_pointer_regexp
425
426 check_var_with_bool_opt "symbols" "a_point_t"
427 check_var_with_bool_opt "symbols" "a_point_t_pointer"
428 check_var_with_bool_opt "symbols" "another_point"
429 check_var_with_bool_opt "symbols" "a_struct_with_union"
430 check_var_with_bool_opt "symbols" "an_enum"
431 check_var_with_bool_opt "symbols" "a_string"
432 check_var_with_bool_opt "symbols" "a_binary_string"
433 check_var_with_bool_opt "symbols" "a_binary_string_array"
434 check_var_with_bool_opt "symbols" "a_big_string"
435 check_var_with_bool_opt "symbols" "an_array"
436 check_var_with_bool_opt "symbols" "an_array_with_repetition"
437 check_var_with_bool_opt "symbols" "a_symbol_pointer" \
438 $undefined \
439 $default_pointer_regexp
440
441 if { $current_lang == "c++" } {
442 check_var_with_bool_opt "symbols" "a_point_t_ref"
443 check_var_with_bool_opt "symbols" "a_base_ref"
444 }
445
446 with_temp_option "set print symbol off" "set print symbol on" {
447 check_var_with_no_opts "a_symbol_pointer" \
448 $default_pointer_regexp
449 check_var_with_bool_opt "symbols" "a_symbol_pointer" \
450 $undefined \
451 $default_pointer_regexp
452 }
453}
454
455# Test the unions option for gdb.Value.format_string.
5fc5a1b8 456proc_with_prefix test_unions {} {
52093e1b
MB
457 global undefined
458 global current_lang
459
460 check_var_with_bool_opt "unions" "a_point_t"
461 check_var_with_bool_opt "unions" "a_point_t_pointer"
462 check_var_with_bool_opt "unions" "another_point"
463 check_var_with_bool_opt "unions" "a_struct_with_union" \
464 $undefined \
465 "\\{the_union = \\{...\\}\\}"
466 check_var_with_bool_opt "unions" "an_enum"
467 check_var_with_bool_opt "unions" "a_string"
468 check_var_with_bool_opt "unions" "a_binary_string"
469 check_var_with_bool_opt "unions" "a_binary_string_array"
470 check_var_with_bool_opt "unions" "a_big_string"
471 check_var_with_bool_opt "unions" "an_array"
472 check_var_with_bool_opt "unions" "an_array_with_repetition"
473 check_var_with_bool_opt "unions" "a_symbol_pointer"
474
475 if { $current_lang == "c++" } {
476 check_var_with_bool_opt "unions" "a_point_t_ref"
477 check_var_with_bool_opt "unions" "a_base_ref"
478 }
479
480 with_temp_option "set print union off" "set print union on" {
481 check_var_with_no_opts "a_struct_with_union" \
482 "\\{the_union = \\{...\\}\\}"
483 check_var_with_bool_opt "unions" "a_struct_with_union" \
484 $undefined \
485 "\\{the_union = \\{...\\}\\}"
486 }
487}
488
4aea001f 489# Test the address option for gdb.Value.format_string.
5fc5a1b8 490proc_with_prefix test_address {} {
4aea001f
HD
491 global undefined
492 global current_lang
493
494 check_var_with_bool_opt "address" "a_point_t"
495 check_var_with_bool_opt "address" "a_point_t_pointer" \
496 $undefined \
497 ""
498 check_var_with_bool_opt "address" "another_point"
499 check_var_with_bool_opt "symbols" "a_struct_with_union"
500 check_var_with_bool_opt "address" "an_enum"
501 check_var_with_bool_opt "address" "a_string" \
502 $undefined \
503 "\"hello world\""
504 check_var_with_bool_opt "address" "a_binary_string" \
505 $undefined \
506 "\"hello\""
507 check_var_with_bool_opt "address" "a_binary_string_array"
508 check_var_with_bool_opt "address" "a_big_string"
509 check_var_with_bool_opt "address" "an_array"
510 check_var_with_bool_opt "address" "an_array_with_repetition"
511 check_var_with_bool_opt "address" "a_symbol_pointer" \
512 $undefined \
513 "<global_symbol>"
514
515 if { $current_lang == "c++" } {
516 check_var_with_bool_opt "address" "a_point_t_ref"
517 check_var_with_bool_opt "address" "a_base_ref" \
518 $undefined \
519 ""
520 }
521
522 with_temp_option "set print address off" "set print address on" {
523 check_var_with_no_opts "a_string" \
524 "\"hello world\""
525 check_var_with_bool_opt "address" "a_string" \
526 $undefined \
527 "\"hello world\""
528 }
529}
530
3f52a090
EL
531# Test the nibbles option for gdb.Value.format_string.
532proc_with_prefix test_nibbles {} {
533 global current_lang
534
535 set opts "format='t', nibbles=True"
536 with_test_prefix $opts {
537 if { $current_lang == "c" } {
538 set binary_pointer_regexp "\[ 0-1\]+"
539 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
540 "0010 1010" \
541 "42 with option ${opts}"
542
c4a3dbaf
TT
543 check_format_string "a_point_t" $opts \
544 [string_to_regexp "Pretty Point (0010 1010, 1100)"]
3f52a090
EL
545 check_format_string "a_point_t_pointer" $opts \
546 $binary_pointer_regexp
c4a3dbaf
TT
547 check_format_string "another_point" $opts \
548 [string_to_regexp "Pretty Point (0111 1011, 0001 1100 1000)"]
3f52a090
EL
549
550 check_format_string "a_struct_with_union" $opts \
551 "\\{the_union = \\{an_int = 0010 1010 0010 1010 0010 1010 0010 1010, a_char = 0010 1010\\}\\}"
552 check_format_string "an_enum" $opts \
553 "0001"
554 check_format_string "a_string" $opts \
555 $binary_pointer_regexp
556 check_format_string "a_binary_string" $opts \
557 $binary_pointer_regexp
558 check_format_string "a_binary_string_array" $opts \
559 "\\{0110 1000, 0110 0101, 0110 1100, 0110 1100, 0110 1111, 0, 0111 0111, 0110 1111, 0111 0010, 0110 1100, 0110 0100, 0\\}"
560 check_format_string "a_big_string" $opts \
561 "\\{0100 0001, 0100 0010, 0100 0011, 0100 0100, 0100 0101, \[, 0-1\]+\.\.\.\\}"
562 check_format_string "an_array" $opts \
563 "\\{0010, 0011, 0101\\}"
564 check_format_string "an_array_with_repetition" $opts \
565 "\\{0001, 0011 <repeats 12 times>, 0101, 0101, 0101\\}"
566 check_format_string "a_symbol_pointer" $opts \
567 $binary_pointer_regexp
568 }
569 if { $current_lang == "c++" } {
570 set binary_pointer_regexp "\['0-1\]+"
571 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
572 "0010'1010" \
573 "42 with option ${opts}"
574
c4a3dbaf
TT
575 check_format_string "a_point_t" $opts \
576 [string_to_regexp "Pretty Point (0010'1010, 1100)"]
3f52a090
EL
577 check_format_string "a_point_t_pointer" $opts \
578 $binary_pointer_regexp
c4a3dbaf
TT
579 check_format_string "another_point" $opts \
580 [string_to_regexp "Pretty Point (0111'1011, 0001'1100'1000)"]
3f52a090
EL
581
582 check_format_string "a_struct_with_union" $opts \
583 "\\{the_union = \\{an_int = 0010'1010'0010'1010'0010'1010'0010'1010, a_char = 0010'1010\\}\\}"
584 check_format_string "an_enum" $opts \
585 "0001"
586 check_format_string "a_string" $opts \
587 $binary_pointer_regexp
588 check_format_string "a_binary_string" $opts \
589 $binary_pointer_regexp
590 check_format_string "a_binary_string_array" $opts \
591 "\\{0110'1000, 0110'0101, 0110'1100, 0110'1100, 0110'1111, 0, 0111'0111, 0110'1111, 0111'0010, 0110'1100, 0110'0100, 0\\}"
592 check_format_string "a_big_string" $opts \
593 "\\{0100'0001, 0100'0010, 0100'0011, 0100'0100, 0100'0101, \[, '0-1\]+\.\.\.\\}"
594 check_format_string "an_array" $opts \
595 "\\{0010, 0011, 0101\\}"
596 check_format_string "an_array_with_repetition" $opts \
597 "\\{0001, 0011 <repeats 12 times>, 0101, 0101, 0101\\}"
598 check_format_string "a_symbol_pointer" $opts \
599 $binary_pointer_regexp
600
c4a3dbaf
TT
601 check_format_string "a_point_t_ref" $opts \
602 [string_to_regexp "Pretty Point (0010'1010, 1100)"]
3f52a090
EL
603 check_format_string "a_base_ref" $opts
604 }
605 }
606}
607
52093e1b 608# Test the deref_refs option for gdb.Value.format_string.
5fc5a1b8 609proc_with_prefix test_deref_refs {} {
52093e1b
MB
610 global current_lang
611 global default_pointer_regexp
612 global default_ref_regexp
7c794afd 613 global decimal
52093e1b
MB
614
615 check_var_with_bool_opt "deref_refs" "a_point_t"
616 check_var_with_bool_opt "deref_refs" "a_point_t_pointer"
617 check_var_with_bool_opt "deref_refs" "another_point"
618 check_var_with_bool_opt "deref_refs" "a_struct_with_union"
619 check_var_with_bool_opt "deref_refs" "an_enum"
620 check_var_with_bool_opt "deref_refs" "a_string"
621 check_var_with_bool_opt "deref_refs" "a_binary_string"
622 check_var_with_bool_opt "deref_refs" "a_binary_string_array"
623 check_var_with_bool_opt "deref_refs" "a_big_string"
624 check_var_with_bool_opt "deref_refs" "an_array"
625 check_var_with_bool_opt "deref_refs" "an_array_with_repetition"
626 check_var_with_bool_opt "deref_refs" "a_symbol_pointer"
627
628 if { $current_lang == "c++" } {
629 check_var_with_bool_opt "deref_refs" "a_point_t_ref"
630 check_var_with_bool_opt "deref_refs" "a_base_ref" \
7c794afd 631 "${default_ref_regexp}: \\{_vptr\[.\$\]Base = ${default_pointer_regexp} <vtable for Deriv\\+$decimal>, a = 42, static a_static_member = 2019\\}"
52093e1b
MB
632 }
633}
634
635# Test the actual_objects option for gdb.Value.format_string.
5fc5a1b8 636proc_with_prefix test_actual_objects {} {
52093e1b
MB
637 global current_lang
638
639 check_var_with_bool_opt "actual_objects" "a_point_t"
640 check_var_with_bool_opt "actual_objects" "a_point_t_pointer"
641 check_var_with_bool_opt "actual_objects" "another_point"
642 check_var_with_bool_opt "actual_objects" "a_struct_with_union"
643 check_var_with_bool_opt "actual_objects" "an_enum"
644 check_var_with_bool_opt "actual_objects" "a_string"
645 check_var_with_bool_opt "actual_objects" "a_binary_string"
646 check_var_with_bool_opt "actual_objects" "a_binary_string_array"
647 check_var_with_bool_opt "actual_objects" "a_big_string"
648 check_var_with_bool_opt "actual_objects" "an_array"
649 check_var_with_bool_opt "actual_objects" "an_array_with_repetition"
650 check_var_with_bool_opt "actual_objects" "a_symbol_pointer"
651
652 if { $current_lang == "c++" } {
653 # Nothing changes in all of the C++ tests because deref_refs is not
654 # True.
655 check_var_with_bool_opt "actual_objects" "a_point_t_ref"
656 check_var_with_bool_opt "actual_objects" "a_base_ref"
657
658 with_temp_option "set print object on" "set print object off" {
659 check_var_with_no_opts "a_point_t_ref"
660 check_var_with_bool_opt "actual_objects" "a_point_t_ref"
661
662 check_var_with_no_opts "a_base_ref"
663 check_var_with_bool_opt "actual_objects" "a_base_ref"
664 }
665 }
666}
667
668# Test the static_members option for gdb.Value.format_string.
5fc5a1b8 669proc_with_prefix test_static_members {} {
52093e1b
MB
670 global current_lang
671
672 check_var_with_bool_opt "static_members" "a_point_t"
673 check_var_with_bool_opt "static_members" "a_point_t_pointer"
674 check_var_with_bool_opt "static_members" "another_point"
675 check_var_with_bool_opt "static_members" "a_struct_with_union"
676 check_var_with_bool_opt "static_members" "an_enum"
677 check_var_with_bool_opt "static_members" "a_string"
678 check_var_with_bool_opt "static_members" "a_binary_string"
679 check_var_with_bool_opt "static_members" "a_binary_string_array"
680 check_var_with_bool_opt "static_members" "a_big_string"
681 check_var_with_bool_opt "static_members" "an_array"
682 check_var_with_bool_opt "static_members" "an_array_with_repetition"
683 check_var_with_bool_opt "static_members" "a_symbol_pointer"
684
685 if { $current_lang == "c++" } {
686 # Nothing changes in all of the C++ tests because deref_refs is not
687 # True.
688 check_var_with_bool_opt "static_members" "a_point_t_ref"
689 check_var_with_bool_opt "static_members" "a_base_ref"
690
691 with_temp_option \
692 "set print static-members off" \
693 "set print static-members on" {
694 check_var_with_no_opts "a_point_t_ref"
695 check_var_with_bool_opt "static_members" "a_point_t_ref"
696
697 check_var_with_no_opts "a_base_ref"
698 check_var_with_bool_opt "static_members" "a_base_ref"
699 }
700 }
701}
702
703# Test the max_elements option for gdb.Value.format_string.
77519ab3
AB
704# SETTING is the print setting to verify, either "elements"
705# or "characters". UNLIMITED is the numeric value to use
706# for the "unlimited" setting.
707
708proc test_max_string_one { setting unlimited } {
52093e1b
MB
709 global current_lang
710 global default_pointer_regexp
711
712 # 200 is the default maximum number of elements, so setting it should
713 # not change the output.
77519ab3 714 set opts "max_$setting=200"
52093e1b
MB
715 with_test_prefix $opts {
716 check_format_string "a_point_t" $opts
717 check_format_string "a_point_t_pointer" $opts
718 check_format_string "another_point" $opts
719 check_format_string "a_struct_with_union" $opts
720 check_format_string "an_enum" $opts
721 check_format_string "a_string" $opts
722 check_format_string "a_binary_string" $opts
723 check_format_string "a_binary_string_array" $opts
724 check_format_string "a_big_string" $opts
77519ab3
AB
725 if { $setting == "elements" } {
726 check_format_string "an_array" $opts
727 check_format_string "an_array_with_repetition" $opts
728 }
52093e1b
MB
729 check_format_string "a_symbol_pointer" $opts
730
731 if { $current_lang == "c++" } {
732 check_format_string "a_point_t_ref" $opts
733 check_format_string "a_base_ref" $opts
734 }
735 }
736
77519ab3 737 set opts "max_$setting=3"
52093e1b
MB
738 with_test_prefix $opts {
739 check_format_string "a_point_t" $opts
740 check_format_string "a_point_t_pointer" $opts
741 check_format_string "another_point" $opts
742 check_format_string "a_struct_with_union" $opts
743 check_format_string "an_enum" $opts
744 check_format_string "a_string" $opts \
745 "${default_pointer_regexp} \"hel\"..."
746 check_format_string "a_binary_string" $opts \
747 "${default_pointer_regexp} \"hel\"..."
52093e1b 748 check_format_string "a_binary_string_array" $opts \
d2b585f3 749 "\"hel\"..."
52093e1b
MB
750 check_format_string "a_big_string" $opts \
751 [get_cut_big_string 3]
77519ab3
AB
752 if { $setting == "elements"} {
753 check_format_string "an_array" $opts
754 check_format_string "an_array_with_repetition" $opts \
755 "\\{1, 3 <repeats 12 times>...\\}"
756 }
52093e1b
MB
757 check_format_string "a_symbol_pointer" $opts
758
759 if { $current_lang == "c++" } {
760 check_format_string "a_point_t_ref" $opts
761 check_format_string "a_base_ref" $opts
762 }
763 }
764
77519ab3 765 # Both 1,000 (we don't have that many elements) and unlimited should
52093e1b 766 # mean no truncation.
77519ab3 767 foreach opts [list "max_$setting=1000" "max_$setting=$unlimited"] {
52093e1b
MB
768 with_test_prefix $opts {
769 check_format_string "a_point_t" $opts
770 check_format_string "a_point_t_pointer" $opts
771 check_format_string "another_point" $opts
772 check_format_string "a_struct_with_union" $opts
773 check_format_string "an_enum" $opts
774 check_format_string "a_string" $opts
775 check_format_string "a_binary_string" $opts
776 check_format_string "a_binary_string_array" $opts
777 check_format_string "a_big_string" $opts \
778 [get_cut_big_string 1000]
77519ab3
AB
779 if { $setting == "elements"} {
780 check_format_string "an_array" $opts
781 check_format_string "an_array_with_repetition" $opts
782 }
52093e1b
MB
783 check_format_string "a_symbol_pointer" $opts
784
785 if { $current_lang == "c++" } {
786 check_format_string "a_point_t_ref" $opts
787 check_format_string "a_base_ref" $opts
788 }
789 }
790 }
791
77519ab3 792 with_temp_option "set print $setting 4" "set print $setting 200" {
52093e1b
MB
793 check_format_string "a_string" "" \
794 "${default_pointer_regexp} \"hell\"..."
795 check_format_string "a_binary_string" "" \
796 "${default_pointer_regexp} \"hell\"..."
797 check_format_string "a_binary_string_array" "" \
798 "\"hell\"..."
77519ab3
AB
799 if { $setting == "elements"} {
800 check_format_string "an_array_with_repetition" "" \
801 "\\{1, 3 <repeats 12 times>...\\}"
802 }
803 }
804}
805
806proc_with_prefix test_max_string {} {
807 foreach_with_prefix setting { "elements" "characters" } {
808 test_max_string_one $setting \
809 [string map {elements 0 characters 4294967295} $setting]
52093e1b
MB
810 }
811}
812
2e62ab40 813# Test the max_depth option for gdb.Value.format_string.
5fc5a1b8 814proc_with_prefix test_max_depth {} {
2e62ab40
AB
815 set opts "max_depth=-1"
816 with_test_prefix $opts {
817 check_format_string "a_struct_with_union" $opts
ecf25064
KC
818 check_format_string "a_point_t" $opts "Pretty Point \\(42, 12\\)"
819 check_format_string "a_struct_with_point" $opts
2e62ab40
AB
820 }
821 set opts "max_depth=0"
822 with_test_prefix $opts {
823 check_format_string "a_struct_with_union" $opts "\\{\.\.\.\\}"
ecf25064
KC
824 check_format_string "a_point_t" $opts "Pretty Point \\(42, 12\\)"
825 check_format_string "a_struct_with_point" $opts "\\{\.\.\.\\}"
2e62ab40
AB
826 }
827 set opts "max_depth=1"
828 with_test_prefix $opts {
829 check_format_string "a_struct_with_union" $opts "\\{the_union = \\{\.\.\.\\}\\}"
ecf25064
KC
830 check_format_string "a_point_t" $opts "Pretty Point \\(42, 12\\)"
831 check_format_string "a_struct_with_point" $opts
2e62ab40
AB
832 }
833 set opts "max_depth=2"
834 with_test_prefix $opts {
835 check_format_string "a_struct_with_union" $opts
ecf25064
KC
836 check_format_string "a_point_t" $opts "Pretty Point \\(42, 12\\)"
837 check_format_string "a_struct_with_point" $opts
2e62ab40
AB
838 }
839}
840
52093e1b 841# Test the repeat_threshold option for gdb.Value.format_string.
5fc5a1b8 842proc_with_prefix test_repeat_threshold {} {
52093e1b
MB
843 global current_lang
844 global default_pointer_regexp
845
846 # 10 is the default threshold for repeated items, so setting it should
847 # not change the output.
848 set opts "repeat_threshold=10"
849 with_test_prefix $opts {
850 check_format_string "a_point_t" $opts
851 check_format_string "a_point_t_pointer" $opts
852 check_format_string "another_point" $opts
853 check_format_string "a_struct_with_union" $opts
854 check_format_string "an_enum" $opts
855 check_format_string "a_string" $opts
856 check_format_string "a_binary_string" $opts
857 check_format_string "a_binary_string_array" $opts
858 check_format_string "a_big_string" $opts
859 check_format_string "an_array" $opts
860 check_format_string "an_array_with_repetition" $opts
861 check_format_string "a_symbol_pointer" $opts
862
863 if { $current_lang == "c++" } {
864 check_format_string "a_point_t_ref" $opts
865 check_format_string "a_base_ref" $opts
866 }
867 }
868
869 set opts "repeat_threshold=1"
870 with_test_prefix $opts {
871 check_format_string "a_point_t" $opts
872 check_format_string "a_point_t_pointer" $opts
873 check_format_string "another_point" $opts
874 check_format_string "a_struct_with_union" $opts
875 check_format_string "an_enum" $opts
876 check_format_string "a_string" $opts \
877 "${default_pointer_regexp} \"he\", 'l' <repeats 2 times>, \"o world\""
878 check_format_string "a_binary_string" $opts \
879 "${default_pointer_regexp} \"he\", 'l' <repeats 2 times>, \"o\""
880 check_format_string "a_binary_string_array" $opts \
881 "\"he\", 'l' <repeats 2 times>, \"o\\\\000world\""
882 check_format_string "a_big_string" $opts
883 check_format_string "an_array" $opts
884 check_format_string "an_array_with_repetition" $opts \
885 "\\{1, 3 <repeats 12 times>, 5 <repeats 3 times>\\}"
886
887 check_format_string "a_symbol_pointer" $opts
888
889 if { $current_lang == "c++" } {
890 check_format_string "a_point_t_ref" $opts
891 check_format_string "a_base_ref" $opts
892 }
893 }
894
895 set opts "repeat_threshold=3"
896 with_test_prefix $opts {
897 check_format_string "a_point_t" $opts
898 check_format_string "a_point_t_pointer" $opts
899 check_format_string "another_point" $opts
900 check_format_string "a_struct_with_union" $opts
901 check_format_string "an_enum" $opts
902 check_format_string "a_string" $opts
903 check_format_string "a_binary_string" $opts
904 check_format_string "a_binary_string_array" $opts
905 check_format_string "a_big_string" $opts
906 check_format_string "an_array" $opts
907 check_format_string "an_array_with_repetition" $opts
908 check_format_string "a_symbol_pointer" $opts
909
910 if { $current_lang == "c++" } {
911 check_format_string "a_point_t_ref" $opts
912 check_format_string "a_base_ref" $opts
913 }
914 }
915
916 # Both 100 (we don't have that many repeated elements) and 0 (unlimited)
917 # should mean no truncation.
918 foreach opts { "repeat_threshold=100" "repeat_threshold=0" } {
919 with_test_prefix $opts {
920 check_format_string "a_point_t" $opts
921 check_format_string "a_point_t_pointer" $opts
922 check_format_string "another_point" $opts
923 check_format_string "a_struct_with_union" $opts
924 check_format_string "an_enum" $opts
925 check_format_string "a_string" $opts
926 check_format_string "a_binary_string" $opts
927 check_format_string "a_binary_string_array" $opts
928 check_format_string "a_big_string" $opts
929 check_format_string "an_array" $opts
930 check_format_string "an_array_with_repetition" $opts \
931 "\\{1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5\\}"
932 check_format_string "a_symbol_pointer" $opts
933
934 if { $current_lang == "c++" } {
935 check_format_string "a_point_t_ref" $opts
936 check_format_string "a_base_ref" $opts
937 }
938 }
939 }
940
941 with_temp_option "set print repeats 1" "set print repeats 10" {
942 check_format_string "an_array_with_repetition" "" \
943 "\\{1, 3 <repeats 12 times>, 5 <repeats 3 times>\\}"
944 }
945}
946
947# Test the format option for gdb.Value.format_string.
5fc5a1b8 948proc_with_prefix test_format {} {
52093e1b
MB
949 global current_lang
950 global default_pointer_regexp
951
952 # Hexadecimal.
953 set opts "format='x'"
954 with_test_prefix $opts {
955 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
956 "0x2a" \
957 "42 with option ${opts}"
958
c4a3dbaf
TT
959 check_format_string "a_point_t" $opts \
960 "Pretty Point \\(0x2a, 0xc\\)"
52093e1b 961 check_format_string "a_point_t_pointer" $opts
c4a3dbaf
TT
962 check_format_string "another_point" $opts \
963 "Pretty Point \\(0x7b, 0x1c8\\)"
52093e1b 964 check_format_string "a_struct_with_union" $opts \
9ef62df0 965 "\\{the_union = \\{an_int = 0x2a2a2a2a, a_char = 0x2a\\}\\}"
52093e1b
MB
966 check_format_string "an_enum" $opts \
967 "0x1"
968 check_format_string "a_string" $opts \
969 $default_pointer_regexp
970 check_format_string "a_binary_string" $opts \
971 $default_pointer_regexp
972 check_format_string "a_binary_string_array" $opts \
973 "\\{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0\\}"
974 check_format_string "a_big_string" $opts \
975 "\\{0x41, 0x42, 0x43, 0x44, 0x45, \[, x0-9a-f\]+\.\.\.\\}"
976 check_format_string "an_array" $opts \
977 "\\{0x2, 0x3, 0x5\\}"
978 check_format_string "an_array_with_repetition" $opts \
979 "\\{0x1, 0x3 <repeats 12 times>, 0x5, 0x5, 0x5\\}"
980 check_format_string "a_symbol_pointer" $opts \
981 $default_pointer_regexp
982
983 if { $current_lang == "c++" } {
c4a3dbaf
TT
984 check_format_string "a_point_t_ref" $opts \
985 "Pretty Point \\(0x2a, 0xc\\)"
52093e1b
MB
986 check_format_string "a_base_ref" $opts
987 }
988 }
989
9d748d02
EL
990 # Binary.
991 set opts "format='t'"
992 with_test_prefix $opts {
993 set binary_pointer_regexp "\[0-1\]+"
994 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
995 "101010" \
996 "42 with option ${opts}"
997
c4a3dbaf
TT
998 check_format_string "a_point_t" $opts \
999 "Pretty Point \\(101010, 1100\\)"
9d748d02
EL
1000 check_format_string "a_point_t_pointer" $opts \
1001 $binary_pointer_regexp
c4a3dbaf
TT
1002 check_format_string "another_point" $opts \
1003 "Pretty Point \\(1111011, 111001000\\)"
9d748d02
EL
1004 check_format_string "a_struct_with_union" $opts \
1005 "\\{the_union = \\{an_int = 101010001010100010101000101010, a_char = 101010\\}\\}"
1006 check_format_string "an_enum" $opts \
1007 "1"
1008 check_format_string "a_string" $opts \
1009 $binary_pointer_regexp
1010 check_format_string "a_binary_string" $opts \
1011 $binary_pointer_regexp
1012 check_format_string "a_binary_string_array" $opts \
1013 "\\{1101000, 1100101, 1101100, 1101100, 1101111, 0, 1110111, 1101111, 1110010, 1101100, 1100100, 0\\}"
1014 check_format_string "a_big_string" $opts \
1015 "\\{1000001, 1000010, 1000011, 1000100, 1000101, \[, 0-1\]+\.\.\.\\}"
1016 check_format_string "an_array" $opts \
1017 "\\{10, 11, 101\\}"
1018 check_format_string "an_array_with_repetition" $opts \
1019 "\\{1, 11 <repeats 12 times>, 101, 101, 101\\}"
1020 check_format_string "a_symbol_pointer" $opts \
1021 $binary_pointer_regexp
1022
1023 if { $current_lang == "c++" } {
c4a3dbaf
TT
1024 check_format_string "a_point_t_ref" $opts \
1025 "Pretty Point \\(101010, 1100\\)"
9d748d02
EL
1026 check_format_string "a_base_ref" $opts
1027 }
1028 }
1029
52093e1b
MB
1030 # Decimal.
1031 set opts "format='d'"
1032 with_test_prefix $opts {
1033 set decimal_pointer_regexp "\[0-9\]+"
1034 gdb_test "python print (gdb.Value (0x2a).format_string (${opts}))" \
1035 "42" \
1036 "0x2a with option ${opts}"
1037
1038 check_format_string "a_point_t" $opts
1039 check_format_string "a_point_t_pointer" $opts \
1040 $decimal_pointer_regexp
1041 check_format_string "another_point" $opts
1042 check_format_string "a_struct_with_union" $opts \
9ef62df0 1043 "\\{the_union = \\{an_int = 707406378, a_char = 42\\}\\}"
52093e1b
MB
1044 check_format_string "an_enum" $opts \
1045 "1"
1046 check_format_string "a_string" $opts \
1047 $decimal_pointer_regexp
1048 check_format_string "a_binary_string" $opts \
1049 $decimal_pointer_regexp
1050 check_format_string "a_binary_string_array" $opts \
1051 "\\{104, 101, 108, 108, 111, 0, 119, 111, 114, 108, 100, 0\\}"
1052 check_format_string "a_big_string" $opts \
1053 "\\{65, 66, 67, 68, 69, \[, 0-9\]+\.\.\.\\}"
1054 check_format_string "an_array" $opts
1055 check_format_string "an_array_with_repetition" $opts
1056 check_format_string "a_symbol_pointer" $opts \
1057 $decimal_pointer_regexp
1058
1059 if { $current_lang == "c++" } {
1060 check_format_string "a_point_t_ref" $opts
1061 check_format_string "a_base_ref" $opts
1062 }
1063 }
1064}
1065
1066# Test mixing options.
5fc5a1b8 1067proc_with_prefix test_mixed {} {
52093e1b
MB
1068 global current_lang
1069 global default_ref_regexp
1070 global default_pointer_regexp
7c794afd 1071 global decimal
52093e1b
MB
1072
1073 check_format_string "a_point_t" \
1074 "raw=True, format='x'" \
1075 "\\{x = 0x2a, y = 0xc\\}"
1076
1077 check_format_string "an_array" \
1078 "array_indexes=True, pretty_arrays=True" \
d5cf82c0 1079 "\\{\[\r\n\]+ \\\[0\\\] = 2,\[\r\n\]+ \\\[1\\\] = 3,\[\r\n\]+ \\\[2\\\] = 5\[\r\n\]+\\}"
52093e1b
MB
1080
1081 check_format_string "a_struct_with_union" \
1082 "pretty_structs=True, unions=False" \
1083 "\\{\[\r\n\]+ the_union = \\{\.\.\.\\}\[\r\n\]+\\}"
1084
1085 check_format_string "a_symbol_pointer" \
1086 "symbols=False, format='d'" \
1087 "\[0-9\]+"
1088
1089 if { $current_lang == "c++" } {
1090 check_format_string "a_point_t_ref" \
1091 "deref_refs=True, actual_objects=True, raw=True" \
1092 "${default_ref_regexp}: \\{x = 42, y = 12\\}"
1093
1094 check_format_string "a_base_ref" \
1095 "deref_refs=True, static_members=False" \
7c794afd 1096 "${default_ref_regexp}: \\{_vptr\[.\$\]Base = ${default_pointer_regexp} <vtable for Deriv\\+$decimal>, a = 42\\}"
52093e1b
MB
1097 }
1098}
1099
1100# Test passing invalid arguments to gdb.Value.format_string.
5fc5a1b8 1101proc_with_prefix test_invalid_args {} {
52093e1b
MB
1102 check_format_string \
1103 "a_point_t" \
1104 "12" \
1105 "TypeError: format_string\\(\\) takes 0 positional arguments but 1 were given.*"
1106
1107 check_format_string \
1108 "a_point_t" \
1109 "invalid=True" \
1110 "TypeError: 'invalid' is an invalid keyword argument for this function.*"
1111
1112 check_format_string \
1113 "a_point_t" \
1114 "raw='hello'" \
1115 "TypeError: argument 1 must be bool, not str.*"
1116
1117 check_format_string \
1118 "a_point_t" \
1119 "format='xd'" \
1120 "ValueError: a single character is required.*"
1121}
1122
0642912e
AB
1123# Check the styling argument to format_string. This function needs to
1124# be called with TERM set such that styling can be applied.
1125proc test_styling {} {
1126 gdb_test "python print(gdb.parse_and_eval(\"a_point_t\").format_string(styling=True, raw=True))" \
1127 "{[style x variable] = 42, [style y variable] = 12}"
1128}
1129
c4a3dbaf
TT
1130# Test the gdb.print_options API.
1131proc test_print_options {} {
1132 gdb_test_no_output "set print elements 500"
1133 gdb_test "python print(gdb.print_options()\['max_elements'\])" "500" \
1134 "examine max elements"
1135 gdb_test "python print('format' in gdb.print_options())" "False" \
1136 "examine format"
1137
3028a2db
TT
1138 check_format_string "a_point_t" "format='t', nibbles=True" \
1139 "Pretty Point \\(0010.1010, 1100\\)" \
c4a3dbaf
TT
1140 "print in binary to fetch options"
1141 gdb_test "python print(saved_options\['format'\] == 't')" "True" \
1142 "format was set"
3028a2db
TT
1143 gdb_test "python print(saved_options\['nibbles'\])" "True" \
1144 "nibbles was set"
72be9d6b
TT
1145
1146 check_format_string "a_point_t" "summary=True" \
1147 "No Data" \
1148 "print in summary mode"
1149 gdb_test "python print(saved_options\['summary'\])" "True" \
1150 "summary was set"
c4a3dbaf
TT
1151}
1152
52093e1b 1153# Run all the tests in common for both C and C++.
5fc5a1b8 1154proc_with_prefix test_all_common {} {
52093e1b
MB
1155 # No options.
1156 test_no_opts
1157 # Single options set to True/False.
1158 test_raw
1159 test_pretty_arrays
1160 test_pretty_structs
1161 test_array_indexes
1162 test_symbols
1163 test_unions
4aea001f 1164 test_address
3f52a090 1165 test_nibbles
52093e1b
MB
1166 test_deref_refs
1167 test_actual_objects
1168 test_static_members
77519ab3 1169 test_max_string
2e62ab40 1170 test_max_depth
52093e1b
MB
1171 test_repeat_threshold
1172 test_format
1173 # Multiple options mixed together.
1174 test_mixed
1175 # Various error conditions.
1176 test_invalid_args
c4a3dbaf 1177 test_print_options
52093e1b
MB
1178}
1179
1180# The current language ("c" or "c++" while running tests).
1181set current_lang ""
1182
1183with_test_prefix "format_string" {
1184 # Perform C Tests.
1185 if { [build_inferior "${binfile}" "c"] == 0 } {
1186 with_test_prefix "lang_c" {
4ebfd53d 1187 with_ansi_styling_terminal {
0642912e
AB
1188 # We run all of these tests in an environment where styling
1189 # could work, but we only expect the final call to
1190 # test_styling to actually produce any styled output.
0642912e
AB
1191 set current_lang "c"
1192 prepare_gdb "${binfile}"
1193 test_all_common
1194 test_styling
1195 }
52093e1b
MB
1196 }
1197 }
1198
1199 # Perform C++ Tests.
1200 if { [build_inferior "${binfile}-cxx" "c++"] == 0 } {
1201 with_test_prefix "lang_cpp" {
1202 set current_lang "c++"
1203 prepare_gdb "${binfile}-cxx"
1204 test_all_common
1205 }
1206 }
1207}