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