aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.python/py-format-string.exp
blob: ebb2074ce87f781e4c430a3b38f5ae8bf3426e2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
# Copyright (C) 2009-2019 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This file is part of the GDB testsuite.  It tests the
# gdb.Value.format_string () method.

load_lib gdb-python.exp

standard_testfile

if [get_compiler_info c++] {
    return -1
}

# Build inferior to language specification.
proc build_inferior {exefile lang} {
  global srcdir subdir srcfile testfile hex

  if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" executable "debug $lang"] != "" } {
      untested "failed to compile in $lang mode"
      return -1
  }

  return 0
}

# Restart GDB.
proc prepare_gdb {exefile} {
  global srcdir subdir srcfile testfile hex

  gdb_exit
  gdb_start
  gdb_reinitialize_dir $srcdir/$subdir
  gdb_load ${exefile}

  # Skip all tests if Python scripting is not enabled.
  if { [skip_python_tests] } { continue }

  if ![runto_main] then {
      perror "couldn't run to breakpoint"
      return
  }

  # Load the pretty printer.
  set remote_python_file \
    [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]
  gdb_test_no_output "source ${remote_python_file}" "load python file"

  runto_bp "break here"
}

# Set breakpoint and run to that breakpoint.
proc runto_bp {bp} {
  gdb_breakpoint [gdb_get_line_number $bp]
  gdb_continue_to_breakpoint $bp
}

# Set an option using the GDB command in $set_cmd, execute $body, and then
# restore the option using the GDB command in $unset_cmd.
proc with_temp_option { set_cmd unset_cmd body } {
  with_test_prefix $set_cmd {
    gdb_test "$set_cmd" ".*"
    uplevel 1 $body
    gdb_test "$unset_cmd" ".*"
  }
}

# A regular expression for a pointer.
set default_pointer_regexp "0x\[a-fA-F0-9\]+"

# A regular expression for a non-expanded C++ reference.
#
# Stringifying a C++ reference produces an address preceeded by a "@" in
# Python, but, by default, the C++ reference/class is expanded by the
# GDB print command.
set default_ref_regexp "@${default_pointer_regexp}"

# The whole content of the C variable a_big_string, i.e. the whole English
# alphabet repeated 10 times.
set whole_big_string ""
set alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for {set i 0} {$i < 10} {incr i} {
  append whole_big_string $alphabet
}
unset alphabet

# Produces a potentially cut down version of $whole_big_string like GDB
# would represent it.
# $max is the maximum number of characters allowed in the string (but
# the return value may contain more to accound for the extra quotes and
# "..." added by GDB).
proc get_cut_big_string { max } {
  global whole_big_string

  set whole_size [string length $whole_big_string]
  if { $max > $whole_size } {
    return "\"${whole_big_string}\""
  }

  set cut_string [string range $whole_big_string 0 [expr $max - 1]]
  return "\"${cut_string}\"..."
}

# A dictionary mapping from C variable names to their default string
# representation when using str () or gdb.Value.format_string () with
# no arguments.
# This usually matches what the print command prints if used with no
# options, except for C++ references which are not expanded by
# default in Python.  See the comment above $default_ref_regexp.
set default_regexp_dict [dict create \
  "a_point_t"			"Pretty Point \\(42, 12\\)" \
  "a_point_t_pointer"		$default_pointer_regexp \
  "a_point_t_ref"		"Pretty Point \\(42, 12\\)" \
  "another_point"		"Pretty Point \\(123, 456\\)" \
  "a_struct_with_union"		"\\{the_union = \\{an_int = 42, a_char = 42 '\\*'\\}\\}" \
  "an_enum"			"ENUM_BAR" \
  "a_string"			"${default_pointer_regexp} \"hello world\"" \
  "a_binary_string"		"${default_pointer_regexp} \"hello\"" \
  "a_binary_string_array"	"\"hello\\\\000world\"" \
  "a_big_string"		[get_cut_big_string 200] \
  "an_array"			"\\{2, 3, 5\\}" \
  "an_array_with_repetition"	"\\{1, 3 <repeats 12 times>, 5, 5, 5\\}" \
  "a_symbol_pointer"		"${default_pointer_regexp} <global_symbol>" \
  "a_base_ref"			"${default_ref_regexp}" \
  ]

# A sentinel value to pass to function to get them to use a default value
# instead.
# Note that we cannot use $undefined for default arguments in function
# definitions as we would just get the literal "$undefined" string, so
# we need to repeat the string.
set undefined "\000UNDEFINED\000"

# Return $value if it's not $undefined, otherwise return the default value
# (from $default_regexp_dict) for the variable $var.
proc get_value_or_default { var value } {
  global undefined
  if { $value != $undefined } {
    return $value
  }

  global default_regexp_dict
  return [dict get $default_regexp_dict $var]
}

# Check that using gdb.Value.format_string on the value representing the
# variable $var produces $expected.
proc check_format_string {
	var
	opts
	{ expected "\000UNDEFINED\000" }
	{ name "\000UNDEFINED\000" }
  } {
  global undefined

  set expected [get_value_or_default $var $expected]
  if { $name == $undefined } {
    set name "${var} with option ${opts}"
  }

  gdb_test \
    "python print (gdb.parse_and_eval ('${var}').format_string (${opts}))" \
    $expected \
    $name
}

# Check that printing $var with no options set, produces the expected
# output.
proc check_var_with_no_opts {
	var
	{ expected "\000UNDEFINED\000" }
  } {
  set expected [get_value_or_default $var $expected]

  with_test_prefix "${var}" {
    check_format_string \
      $var \
      "" \
      $expected \
      "no opts"
    # str () should behave like gdb.Value.format_string () with no args.
    gdb_test \
      "python print (str (gdb.parse_and_eval ('${var}')))" \
      $expected \
      "str"
  }
}

# Check that printing $var with $opt set to True and set to False,
# produces the expected output.
proc check_var_with_bool_opt {
	opt
	var
	{ true_expected  "\000UNDEFINED\000" }
	{ false_expected "\000UNDEFINED\000" }
  } {
  set true_expected  [get_value_or_default $var $true_expected]
  set false_expected [get_value_or_default $var $false_expected]

  with_test_prefix "${var} with option ${opt}" {
    # Option set to True.
    check_format_string \
      $var \
      "${opt}=True" \
      $true_expected \
      "${opt}=true"
    # Option set to False.
    check_format_string \
      $var \
      "${opt}=False" \
      $false_expected \
      "${opt}=false"
  }
}

# Test gdb.Value.format_string with no options.
proc test_no_opts {} {
  global current_lang

  check_var_with_no_opts "a_point_t"
  check_var_with_no_opts "a_point_t_pointer"
  check_var_with_no_opts "another_point"
  check_var_with_no_opts "a_struct_with_union"
  check_var_with_no_opts "an_enum"
  check_var_with_no_opts "a_string"
  check_var_with_no_opts "a_binary_string"
  check_var_with_no_opts "a_binary_string_array"
  check_var_with_no_opts "a_big_string"
  check_var_with_no_opts "an_array"
  check_var_with_no_opts "an_array_with_repetition"
  check_var_with_no_opts "a_symbol_pointer"

  if { $current_lang == "c++" } {
    # Nothing changes in all of the C++ tests because deref_refs is not
    # True.
    check_var_with_no_opts "a_point_t_ref"
    check_var_with_no_opts "a_base_ref"
  }
}

# Test the raw option for gdb.Value.format_string.
proc test_raw {} {
  global current_lang
  global default_ref_regexp

  check_var_with_bool_opt "raw" "a_point_t" \
    "{x = 42, y = 12}"
  check_var_with_bool_opt "raw" "a_point_t_pointer"
  check_var_with_bool_opt "raw" "another_point" \
    "{x = 123, y = 456}"
  check_var_with_bool_opt "raw" "a_struct_with_union"
  check_var_with_bool_opt "raw" "an_enum"
  check_var_with_bool_opt "raw" "a_string"
  check_var_with_bool_opt "raw" "a_binary_string"
  check_var_with_bool_opt "raw" "a_binary_string_array"
  check_var_with_bool_opt "raw" "a_big_string"
  check_var_with_bool_opt "raw" "an_array"
  check_var_with_bool_opt "raw" "an_array_with_repetition"
  check_var_with_bool_opt "raw" "a_symbol_pointer"

  if { $current_lang == "c++" } {
    check_var_with_bool_opt "raw" "a_point_t_ref" \
      ${default_ref_regexp}
    check_var_with_bool_opt "raw" "a_base_ref"
  }

  with_temp_option \
	"disable pretty-printer '' test_lookup_function" \
	"enable pretty-printer '' test_lookup_function" {
    check_var_with_no_opts "a_point_t" \
      "{x = 42, y = 12}"
    check_var_with_bool_opt "raw" "a_point_t" \
      "{x = 42, y = 12}" \
      "{x = 42, y = 12}"
  }
}

# Test the pretty_arrays option for gdb.Value.format_string.
proc test_pretty_arrays {} {
  global current_lang

  set an_array_pretty "  \\{2,\[\r\n\]+  3,\[\r\n\]+  5\\}"
  set an_array_with_repetition_pretty \
    "  \\{1,\[\r\n\]+  3 <repeats 12 times>,\[\r\n\]+  5,\[\r\n\]+  5,\[\r\n\]+  5\\}"

  check_var_with_bool_opt "pretty_arrays" "a_point_t"
  check_var_with_bool_opt "pretty_arrays" "a_point_t_pointer"
  check_var_with_bool_opt "pretty_arrays" "another_point"
  check_var_with_bool_opt "pretty_arrays" "a_struct_with_union"
  check_var_with_bool_opt "pretty_arrays" "an_enum"
  check_var_with_bool_opt "pretty_arrays" "a_string"
  check_var_with_bool_opt "pretty_arrays" "a_binary_string"
  check_var_with_bool_opt "pretty_arrays" "a_binary_string_array"
  check_var_with_bool_opt "pretty_arrays" "a_big_string"
  check_var_with_bool_opt "pretty_arrays" "an_array" \
    $an_array_pretty
  check_var_with_bool_opt "pretty_arrays" "an_array_with_repetition" \
    $an_array_with_repetition_pretty
  check_var_with_bool_opt "pretty_arrays" "a_symbol_pointer"

  if { $current_lang == "c++" } {
    check_var_with_bool_opt "pretty_arrays" "a_point_t_ref"
    check_var_with_bool_opt "pretty_arrays" "a_base_ref"
  }

  with_temp_option "set print array on" "set print array off" {
    check_var_with_no_opts "an_array" \
      $an_array_pretty
    check_var_with_bool_opt "pretty_arrays" "an_array" \
      $an_array_pretty

    check_var_with_no_opts "an_array_with_repetition" \
      $an_array_with_repetition_pretty
    check_var_with_bool_opt "pretty_arrays" "an_array_with_repetition" \
      $an_array_with_repetition_pretty
  }
}

# Test the pretty_structs option for gdb.Value.format_string.
proc test_pretty_structs {} {
  global current_lang

  set a_struct_with_union_pretty \
    "\\{\[\r\n\]+  the_union = \\{\[\r\n\]+    an_int = 42,\[\r\n\]+    a_char = 42 '\\*'\[\r\n\]+  \\}\[\r\n\]+\\}"

  check_var_with_bool_opt "pretty_structs" "a_point_t"
  check_var_with_bool_opt "pretty_structs" "a_point_t_pointer"
  check_var_with_bool_opt "pretty_structs" "another_point"
  check_var_with_bool_opt "pretty_structs" "a_struct_with_union" \
    $a_struct_with_union_pretty
  check_var_with_bool_opt "pretty_structs" "an_enum"
  check_var_with_bool_opt "pretty_structs" "a_string"
  check_var_with_bool_opt "pretty_structs" "a_binary_string"
  check_var_with_bool_opt "pretty_structs" "a_binary_string_array"
  check_var_with_bool_opt "pretty_structs" "a_big_string"
  check_var_with_bool_opt "pretty_structs" "an_array"
  check_var_with_bool_opt "pretty_structs" "an_array_with_repetition"
  check_var_with_bool_opt "pretty_structs" "a_symbol_pointer"

  if { $current_lang == "c++" } {
    check_var_with_bool_opt "pretty_structs" "a_point_t_ref"
    check_var_with_bool_opt "pretty_structs" "a_base_ref"
  }

  with_temp_option "set print structs on" "set print structs off" {
    check_var_with_no_opts "a_struct_with_union"
    check_var_with_bool_opt "pretty_structs" "a_struct_with_union" \
      $a_struct_with_union_pretty
  }

  # point_t is usually printed through the pretty printer.
  # Try disabling it.
  with_temp_option \
	"disable pretty-printer '' test_lookup_function" \
	"enable pretty-printer '' test_lookup_function" {
    check_var_with_no_opts "a_point_t" \
      "{x = 42, y = 12}"
    check_var_with_bool_opt "pretty_structs" "a_point_t" \
      "\\{\[\r\n\]+  x = 42, *\[\r\n\]+  y = 12\[\r\n\]+\\}" \
      "{x = 42, y = 12}" \
  }
}

# Test the array_indexes option for gdb.Value.format_string.
proc test_array_indexes {} {
  global current_lang

  set an_array_with_indexes "\\{\\\[0\\\] = 2, \\\[1\\\] = 3, \\\[2\\\] = 5\\}"
  set an_array_with_repetition_with_indexes \
    "\\{\\\[0\\\] = 1, \\\[1\\\] = 3 <repeats 12 times>, \\\[13\\\] = 5, \\\[14\\\] = 5, \\\[15\\\] = 5\\}"

  check_var_with_bool_opt "array_indexes" "a_point_t"
  check_var_with_bool_opt "array_indexes" "a_point_t_pointer"
  check_var_with_bool_opt "array_indexes" "another_point"
  check_var_with_bool_opt "array_indexes" "a_struct_with_union"
  check_var_with_bool_opt "array_indexes" "an_enum"
  check_var_with_bool_opt "array_indexes" "a_string"
  check_var_with_bool_opt "array_indexes" "a_binary_string"
  check_var_with_bool_opt "array_indexes" "a_binary_string_array"
  check_var_with_bool_opt "array_indexes" "a_big_string"
  check_var_with_bool_opt "array_indexes" "an_array" \
    $an_array_with_indexes
  check_var_with_bool_opt "array_indexes" "an_array_with_repetition" \
    $an_array_with_repetition_with_indexes
  check_var_with_bool_opt "array_indexes" "a_symbol_pointer"

  if { $current_lang == "c++" } {
    check_var_with_bool_opt "array_indexes" "a_point_t_ref"
    check_var_with_bool_opt "array_indexes" "a_base_ref"
  }

  with_temp_option \
	"set print array-indexes on" \
	"set print array-indexes off" {
    check_var_with_no_opts "an_array" \
      $an_array_with_indexes
    check_var_with_bool_opt "array_indexes" "an_array" \
      $an_array_with_indexes

    check_var_with_no_opts "an_array_with_repetition" \
      $an_array_with_repetition_with_indexes
    check_var_with_bool_opt "array_indexes" "an_array_with_repetition" \
      $an_array_with_repetition_with_indexes
  }
}

# Test the symbols option for gdb.Value.format_string.
proc test_symbols {} {
  global undefined
  global current_lang
  global default_pointer_regexp

  check_var_with_bool_opt "symbols" "a_point_t"
  check_var_with_bool_opt "symbols" "a_point_t_pointer"
  check_var_with_bool_opt "symbols" "another_point"
  check_var_with_bool_opt "symbols" "a_struct_with_union"
  check_var_with_bool_opt "symbols" "an_enum"
  check_var_with_bool_opt "symbols" "a_string"
  check_var_with_bool_opt "symbols" "a_binary_string"
  check_var_with_bool_opt "symbols" "a_binary_string_array"
  check_var_with_bool_opt "symbols" "a_big_string"
  check_var_with_bool_opt "symbols" "an_array"
  check_var_with_bool_opt "symbols" "an_array_with_repetition"
  check_var_with_bool_opt "symbols" "a_symbol_pointer" \
    $undefined \
    $default_pointer_regexp

  if { $current_lang == "c++" } {
    check_var_with_bool_opt "symbols" "a_point_t_ref"
    check_var_with_bool_opt "symbols" "a_base_ref"
  }

  with_temp_option "set print symbol off" "set print symbol on" {
    check_var_with_no_opts "a_symbol_pointer" \
      $default_pointer_regexp
    check_var_with_bool_opt "symbols" "a_symbol_pointer" \
      $undefined \
      $default_pointer_regexp
  }
}

# Test the unions option for gdb.Value.format_string.
proc test_unions {} {
  global undefined
  global current_lang

  check_var_with_bool_opt "unions" "a_point_t"
  check_var_with_bool_opt "unions" "a_point_t_pointer"
  check_var_with_bool_opt "unions" "another_point"
  check_var_with_bool_opt "unions" "a_struct_with_union" \
    $undefined \
    "\\{the_union = \\{...\\}\\}"
  check_var_with_bool_opt "unions" "an_enum"
  check_var_with_bool_opt "unions" "a_string"
  check_var_with_bool_opt "unions" "a_binary_string"
  check_var_with_bool_opt "unions" "a_binary_string_array"
  check_var_with_bool_opt "unions" "a_big_string"
  check_var_with_bool_opt "unions" "an_array"
  check_var_with_bool_opt "unions" "an_array_with_repetition"
  check_var_with_bool_opt "unions" "a_symbol_pointer"

  if { $current_lang == "c++" } {
    check_var_with_bool_opt "unions" "a_point_t_ref"
    check_var_with_bool_opt "unions" "a_base_ref"
  }

  with_temp_option "set print union off" "set print union on" {
    check_var_with_no_opts "a_struct_with_union" \
      "\\{the_union = \\{...\\}\\}"
    check_var_with_bool_opt "unions" "a_struct_with_union" \
      $undefined \
      "\\{the_union = \\{...\\}\\}"
  }
}

# Test the deref_refs option for gdb.Value.format_string.
proc test_deref_refs {} {
  global current_lang
  global default_pointer_regexp
  global default_ref_regexp

  check_var_with_bool_opt "deref_refs" "a_point_t"
  check_var_with_bool_opt "deref_refs" "a_point_t_pointer"
  check_var_with_bool_opt "deref_refs" "another_point"
  check_var_with_bool_opt "deref_refs" "a_struct_with_union"
  check_var_with_bool_opt "deref_refs" "an_enum"
  check_var_with_bool_opt "deref_refs" "a_string"
  check_var_with_bool_opt "deref_refs" "a_binary_string"
  check_var_with_bool_opt "deref_refs" "a_binary_string_array"
  check_var_with_bool_opt "deref_refs" "a_big_string"
  check_var_with_bool_opt "deref_refs" "an_array"
  check_var_with_bool_opt "deref_refs" "an_array_with_repetition"
  check_var_with_bool_opt "deref_refs" "a_symbol_pointer"

  if { $current_lang == "c++" } {
    check_var_with_bool_opt "deref_refs" "a_point_t_ref"
    check_var_with_bool_opt "deref_refs" "a_base_ref" \
      "${default_ref_regexp}: \\{_vptr\\.Base = ${default_pointer_regexp} <vtable for Deriv\\+16>, a = 42, static a_static_member = 2019\\}"
  }
}

# Test the actual_objects option for gdb.Value.format_string.
proc test_actual_objects {} {
  global current_lang

  check_var_with_bool_opt "actual_objects" "a_point_t"
  check_var_with_bool_opt "actual_objects" "a_point_t_pointer"
  check_var_with_bool_opt "actual_objects" "another_point"
  check_var_with_bool_opt "actual_objects" "a_struct_with_union"
  check_var_with_bool_opt "actual_objects" "an_enum"
  check_var_with_bool_opt "actual_objects" "a_string"
  check_var_with_bool_opt "actual_objects" "a_binary_string"
  check_var_with_bool_opt "actual_objects" "a_binary_string_array"
  check_var_with_bool_opt "actual_objects" "a_big_string"
  check_var_with_bool_opt "actual_objects" "an_array"
  check_var_with_bool_opt "actual_objects" "an_array_with_repetition"
  check_var_with_bool_opt "actual_objects" "a_symbol_pointer"

  if { $current_lang == "c++" } {
    # Nothing changes in all of the C++ tests because deref_refs is not
    # True.
    check_var_with_bool_opt "actual_objects" "a_point_t_ref"
    check_var_with_bool_opt "actual_objects" "a_base_ref"

    with_temp_option "set print object on" "set print object off" {
      check_var_with_no_opts "a_point_t_ref"
      check_var_with_bool_opt "actual_objects" "a_point_t_ref"

      check_var_with_no_opts "a_base_ref"
      check_var_with_bool_opt "actual_objects" "a_base_ref"
    }
  }
}

# Test the static_members option for gdb.Value.format_string.
proc test_static_members {} {
  global current_lang

  check_var_with_bool_opt "static_members" "a_point_t"
  check_var_with_bool_opt "static_members" "a_point_t_pointer"
  check_var_with_bool_opt "static_members" "another_point"
  check_var_with_bool_opt "static_members" "a_struct_with_union"
  check_var_with_bool_opt "static_members" "an_enum"
  check_var_with_bool_opt "static_members" "a_string"
  check_var_with_bool_opt "static_members" "a_binary_string"
  check_var_with_bool_opt "static_members" "a_binary_string_array"
  check_var_with_bool_opt "static_members" "a_big_string"
  check_var_with_bool_opt "static_members" "an_array"
  check_var_with_bool_opt "static_members" "an_array_with_repetition"
  check_var_with_bool_opt "static_members" "a_symbol_pointer"

  if { $current_lang == "c++" } {
    # Nothing changes in all of the C++ tests because deref_refs is not
    # True.
    check_var_with_bool_opt "static_members" "a_point_t_ref"
    check_var_with_bool_opt "static_members" "a_base_ref"

    with_temp_option \
	"set print static-members off" \
	"set print static-members on" {
      check_var_with_no_opts "a_point_t_ref"
      check_var_with_bool_opt "static_members" "a_point_t_ref"

      check_var_with_no_opts "a_base_ref"
      check_var_with_bool_opt "static_members" "a_base_ref"
    }
  }
}

# Test the max_elements option for gdb.Value.format_string.
proc test_max_elements {} {
  global current_lang
  global default_pointer_regexp

  # 200 is the default maximum number of elements, so setting it should
  # not change the output.
  set opts "max_elements=200"
  with_test_prefix $opts {
    check_format_string "a_point_t" $opts
    check_format_string "a_point_t_pointer" $opts
    check_format_string "another_point" $opts
    check_format_string "a_struct_with_union" $opts
    check_format_string "an_enum" $opts
    check_format_string "a_string" $opts
    check_format_string "a_binary_string" $opts
    check_format_string "a_binary_string_array" $opts
    check_format_string "a_big_string" $opts
    check_format_string "an_array" $opts
    check_format_string "an_array_with_repetition" $opts
    check_format_string "a_symbol_pointer" $opts

    if { $current_lang == "c++" } {
      check_format_string "a_point_t_ref" $opts
      check_format_string "a_base_ref" $opts
    }
  }

  set opts "max_elements=3"
  with_test_prefix $opts {
    check_format_string "a_point_t" $opts
    check_format_string "a_point_t_pointer" $opts
    check_format_string "another_point" $opts
    check_format_string "a_struct_with_union" $opts
    check_format_string "an_enum" $opts
    check_format_string "a_string" $opts \
      "${default_pointer_regexp} \"hel\"..."
    check_format_string "a_binary_string" $opts \
      "${default_pointer_regexp} \"hel\"..."
    # This will print four characters instead of three, see
    # <https://sourceware.org/bugzilla/show_bug.cgi?id=24331>.
    check_format_string "a_binary_string_array" $opts \
      "\"hell\"..."
    check_format_string "a_big_string" $opts \
      [get_cut_big_string 3]
    check_format_string "an_array" $opts
    check_format_string "an_array_with_repetition" $opts \
      "\\{1, 3 <repeats 12 times>...\\}"
    check_format_string "a_symbol_pointer" $opts

    if { $current_lang == "c++" } {
      check_format_string "a_point_t_ref" $opts
      check_format_string "a_base_ref" $opts
    }
  }

  # Both 1,000 (we don't have that many elements) and 0 (unlimited) should
  # mean no truncation.
  foreach opts { "max_elements=1000" "max_elements=0" } {
    with_test_prefix $opts {
      check_format_string "a_point_t" $opts
      check_format_string "a_point_t_pointer" $opts
      check_format_string "another_point" $opts
      check_format_string "a_struct_with_union" $opts
      check_format_string "an_enum" $opts
      check_format_string "a_string" $opts
      check_format_string "a_binary_string" $opts
      check_format_string "a_binary_string_array" $opts
      check_format_string "a_big_string" $opts \
        [get_cut_big_string 1000]
      check_format_string "an_array" $opts
      check_format_string "an_array_with_repetition" $opts
      check_format_string "a_symbol_pointer" $opts

      if { $current_lang == "c++" } {
        check_format_string "a_point_t_ref" $opts
        check_format_string "a_base_ref" $opts
      }
    }
  }

  with_temp_option "set print elements 4" "set print elements 200" {
    check_format_string "a_string" "" \
      "${default_pointer_regexp} \"hell\"..."
    check_format_string "a_binary_string" "" \
      "${default_pointer_regexp} \"hell\"..."
    check_format_string "a_binary_string_array" "" \
      "\"hell\"..."
    check_format_string "an_array_with_repetition" "" \
      "\\{1, 3 <repeats 12 times>...\\}"
  }
}

# Test the max_depth option for gdb.Value.format_string.
proc test_max_depth {} {
    set opts "max_depth=-1"
    with_test_prefix $opts {
	check_format_string "a_struct_with_union" $opts
    }
    set opts "max_depth=0"
    with_test_prefix $opts {
	check_format_string "a_struct_with_union" $opts "\\{\.\.\.\\}"
    }
    set opts "max_depth=1"
    with_test_prefix $opts {
	check_format_string "a_struct_with_union" $opts "\\{the_union = \\{\.\.\.\\}\\}"
    }
    set opts "max_depth=2"
    with_test_prefix $opts {
	check_format_string "a_struct_with_union" $opts
    }
}

# Test the repeat_threshold option for gdb.Value.format_string.
proc test_repeat_threshold {} {
  global current_lang
  global default_pointer_regexp

  # 10 is the default threshold for repeated items, so setting it should
  # not change the output.
  set opts "repeat_threshold=10"
  with_test_prefix $opts {
    check_format_string "a_point_t" $opts
    check_format_string "a_point_t_pointer" $opts
    check_format_string "another_point" $opts
    check_format_string "a_struct_with_union" $opts
    check_format_string "an_enum" $opts
    check_format_string "a_string" $opts
    check_format_string "a_binary_string" $opts
    check_format_string "a_binary_string_array" $opts
    check_format_string "a_big_string" $opts
    check_format_string "an_array" $opts
    check_format_string "an_array_with_repetition" $opts
    check_format_string "a_symbol_pointer" $opts

    if { $current_lang == "c++" } {
      check_format_string "a_point_t_ref" $opts
      check_format_string "a_base_ref" $opts
    }
  }

  set opts "repeat_threshold=1"
  with_test_prefix $opts {
    check_format_string "a_point_t" $opts
    check_format_string "a_point_t_pointer" $opts
    check_format_string "another_point" $opts
    check_format_string "a_struct_with_union" $opts
    check_format_string "an_enum" $opts
    check_format_string "a_string" $opts \
      "${default_pointer_regexp} \"he\", 'l' <repeats 2 times>, \"o world\""
    check_format_string "a_binary_string" $opts \
      "${default_pointer_regexp} \"he\", 'l' <repeats 2 times>, \"o\""
    check_format_string "a_binary_string_array" $opts \
      "\"he\", 'l' <repeats 2 times>, \"o\\\\000world\""
    check_format_string "a_big_string" $opts
    check_format_string "an_array" $opts
    check_format_string "an_array_with_repetition" $opts \
      "\\{1, 3 <repeats 12 times>, 5 <repeats 3 times>\\}"

    check_format_string "a_symbol_pointer" $opts

    if { $current_lang == "c++" } {
      check_format_string "a_point_t_ref" $opts
      check_format_string "a_base_ref" $opts
    }
  }

  set opts "repeat_threshold=3"
  with_test_prefix $opts {
    check_format_string "a_point_t" $opts
    check_format_string "a_point_t_pointer" $opts
    check_format_string "another_point" $opts
    check_format_string "a_struct_with_union" $opts
    check_format_string "an_enum" $opts
    check_format_string "a_string" $opts
    check_format_string "a_binary_string" $opts
    check_format_string "a_binary_string_array" $opts
    check_format_string "a_big_string" $opts
    check_format_string "an_array" $opts
    check_format_string "an_array_with_repetition" $opts
    check_format_string "a_symbol_pointer" $opts

    if { $current_lang == "c++" } {
      check_format_string "a_point_t_ref" $opts
      check_format_string "a_base_ref" $opts
    }
  }

  # Both 100 (we don't have that many repeated elements) and 0 (unlimited)
  # should mean no truncation.
  foreach opts { "repeat_threshold=100" "repeat_threshold=0" } {
    with_test_prefix $opts {
      check_format_string "a_point_t" $opts
      check_format_string "a_point_t_pointer" $opts
      check_format_string "another_point" $opts
      check_format_string "a_struct_with_union" $opts
      check_format_string "an_enum" $opts
      check_format_string "a_string" $opts
      check_format_string "a_binary_string" $opts
      check_format_string "a_binary_string_array" $opts
      check_format_string "a_big_string" $opts
      check_format_string "an_array" $opts
      check_format_string "an_array_with_repetition" $opts \
        "\\{1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5\\}"
      check_format_string "a_symbol_pointer" $opts

      if { $current_lang == "c++" } {
        check_format_string "a_point_t_ref" $opts
        check_format_string "a_base_ref" $opts
      }
    }
  }

  with_temp_option "set print repeats 1" "set print repeats 10" {
    check_format_string "an_array_with_repetition" "" \
      "\\{1, 3 <repeats 12 times>, 5 <repeats 3 times>\\}"
  }
}

# Test the format option for gdb.Value.format_string.
proc test_format {} {
  global current_lang
  global default_pointer_regexp

  # Hexadecimal.
  set opts "format='x'"
  with_test_prefix $opts {
    gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
      "0x2a" \
      "42 with option ${opts}"

    check_format_string "a_point_t" $opts
    check_format_string "a_point_t_pointer" $opts
    check_format_string "another_point" $opts
    check_format_string "a_struct_with_union" $opts \
      "\\{the_union = \\{an_int = 0x2a, a_char = 0x2a\\}\\}"
    check_format_string "an_enum" $opts \
      "0x1"
    check_format_string "a_string" $opts \
      $default_pointer_regexp
    check_format_string "a_binary_string" $opts \
      $default_pointer_regexp
    check_format_string "a_binary_string_array" $opts \
      "\\{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0\\}"
    check_format_string "a_big_string" $opts \
      "\\{0x41, 0x42, 0x43, 0x44, 0x45, \[, x0-9a-f\]+\.\.\.\\}"
    check_format_string "an_array" $opts \
      "\\{0x2, 0x3, 0x5\\}"
    check_format_string "an_array_with_repetition" $opts \
      "\\{0x1, 0x3 <repeats 12 times>, 0x5, 0x5, 0x5\\}"
    check_format_string "a_symbol_pointer" $opts \
      $default_pointer_regexp

    if { $current_lang == "c++" } {
      check_format_string "a_point_t_ref" $opts
      check_format_string "a_base_ref" $opts
    }
  }

  # Decimal.
  set opts "format='d'"
  with_test_prefix $opts {
    set decimal_pointer_regexp "\[0-9\]+"
    gdb_test "python print (gdb.Value (0x2a).format_string (${opts}))" \
      "42" \
      "0x2a with option ${opts}"

    check_format_string "a_point_t" $opts
    check_format_string "a_point_t_pointer" $opts \
      $decimal_pointer_regexp
    check_format_string "another_point" $opts
    check_format_string "a_struct_with_union" $opts \
      "\\{the_union = \\{an_int = 42, a_char = 42\\}\\}"
    check_format_string "an_enum" $opts \
      "1"
    check_format_string "a_string" $opts \
      $decimal_pointer_regexp
    check_format_string "a_binary_string" $opts \
      $decimal_pointer_regexp
    check_format_string "a_binary_string_array" $opts \
      "\\{104, 101, 108, 108, 111, 0, 119, 111, 114, 108, 100, 0\\}"
    check_format_string "a_big_string" $opts \
      "\\{65, 66, 67, 68, 69, \[, 0-9\]+\.\.\.\\}"
    check_format_string "an_array" $opts
    check_format_string "an_array_with_repetition" $opts
    check_format_string "a_symbol_pointer" $opts \
      $decimal_pointer_regexp

    if { $current_lang == "c++" } {
      check_format_string "a_point_t_ref" $opts
      check_format_string "a_base_ref" $opts
    }
  }
}

# Test mixing options.
proc test_mixed {} {
  global current_lang
  global default_ref_regexp
  global default_pointer_regexp

  check_format_string "a_point_t" \
    "raw=True, format='x'" \
    "\\{x = 0x2a, y = 0xc\\}"

  check_format_string "an_array" \
    "array_indexes=True, pretty_arrays=True" \
    "  \\{\\\[0\\\] = 2,\[\r\n\]+  \\\[1\\\] = 3,\[\r\n\]+  \\\[2\\\] = 5\\}"

  check_format_string "a_struct_with_union" \
    "pretty_structs=True, unions=False" \
    "\\{\[\r\n\]+  the_union = \\{\.\.\.\\}\[\r\n\]+\\}"

  check_format_string "a_symbol_pointer" \
    "symbols=False, format='d'" \
    "\[0-9\]+"

  if { $current_lang == "c++" } {
    check_format_string "a_point_t_ref" \
      "deref_refs=True, actual_objects=True, raw=True" \
      "${default_ref_regexp}: \\{x = 42, y = 12\\}"

    check_format_string "a_base_ref" \
      "deref_refs=True, static_members=False" \
      "${default_ref_regexp}: \\{_vptr\\.Base = ${default_pointer_regexp} <vtable for Deriv\\+16>, a = 42\\}"
  }
}

# Test passing invalid arguments to gdb.Value.format_string.
proc test_invalid_args {} {
  check_format_string \
    "a_point_t" \
    "12" \
    "TypeError: format_string\\(\\) takes 0 positional arguments but 1 were given.*"

  check_format_string \
    "a_point_t" \
    "invalid=True" \
    "TypeError: 'invalid' is an invalid keyword argument for this function.*"

  check_format_string \
    "a_point_t" \
    "raw='hello'" \
    "TypeError: argument 1 must be bool, not str.*"

  check_format_string \
    "a_point_t" \
    "format='xd'" \
    "ValueError: a single character is required.*"
}

# Run all the tests in common for both C and C++.
proc test_all_common {} {
  # No options.
  test_no_opts
  # Single options set to True/False.
  test_raw
  test_pretty_arrays
  test_pretty_structs
  test_array_indexes
  test_symbols
  test_unions
  test_deref_refs
  test_actual_objects
  test_static_members
  test_max_elements
  test_max_depth
  test_repeat_threshold
  test_format
  # Multiple options mixed together.
  test_mixed
  # Various error conditions.
  test_invalid_args
}

# The current language ("c" or "c++" while running tests).
set current_lang ""

with_test_prefix "format_string" {
  # Perform C Tests.
  if { [build_inferior "${binfile}" "c"] == 0 } {
    with_test_prefix "lang_c" {
      set current_lang "c"
      prepare_gdb "${binfile}"
      test_all_common
    }
  }

  # Perform C++ Tests.
  if { [build_inferior "${binfile}-cxx" "c++"] == 0 } {
    with_test_prefix "lang_cpp" {
      set current_lang "c++"
      prepare_gdb "${binfile}-cxx"
      test_all_common
    }
  }
}