From: Daniel Knezevic Date: Wed, 26 Nov 2025 11:23:25 +0000 (+0100) Subject: gdb: Make printing enum types prettier. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=be90f3e2afa841a7e32e4118a6b9104182c6e05b;p=thirdparty%2Fbinutils-gdb.git gdb: Make printing enum types prettier. Update printing of enum types to follow the same formatting conventions as for structs resulting in more readable output. Now that horizontal space is less of an issue enum values are always printed. Empty enums are now printed with an added "" message. Example of printing an enum with default values: enum class TestEnum {A, B, C, D}; (gdb) ptype TestEnum type = enum class TestEnum : int { TestEnum::A = 0, TestEnum::B = 1, TestEnum::C = 2, TestEnum::D = 3 } Example of printing an empty enum: enum class TestEnum {}; (gdb) ptype TestEnum type = enum class TestEnum : int { } Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=19294 Approved-By: Tom Tromey --- diff --git a/gdb/c-typeprint.c b/gdb/c-typeprint.c index c399f3fc235..07d16ed361e 100644 --- a/gdb/c-typeprint.c +++ b/gdb/c-typeprint.c @@ -1325,8 +1325,6 @@ c_type_print_base_enum (struct type *type, struct ui_file *stream, } else if (show > 0 || type->name () == NULL) { - LONGEST lastval = 0; - /* We can't handle this case perfectly, as DWARF does not tell us whether or not the underlying type was specified in the source (and other debug formats don't provide this @@ -1343,25 +1341,27 @@ c_type_print_base_enum (struct type *type, struct ui_file *stream, gdb_printf (stream, ": %s ", underlying->name ()); } - gdb_printf (stream, "{"); + gdb_printf (stream, "{\n"); int len = type->num_fields (); - for (int i = 0; i < len; i++) + if (len == 0) + gdb_printf (stream, "%*s%ps", level + 4, "", + styled_string (metadata_style.style (), + "")); + else { - QUIT; - if (i) - gdb_printf (stream, ", "); - stream->wrap_here (4); - fputs_styled (type->field (i).name (), - variable_name_style.style (), stream); - if (lastval != type->field (i).loc_enumval ()) + for (int i = 0; i < len; i++) { + QUIT; + if (i != 0) + gdb_printf (stream, ",\n"); + gdb_printf (stream, "%*s%ps", level + 4, "", + styled_string (variable_name_style.style (), + type->field (i).name ())); gdb_printf (stream, " = %s", plongest (type->field (i).loc_enumval ())); - lastval = type->field (i).loc_enumval (); } - lastval++; } - gdb_printf (stream, "}"); + gdb_printf (stream, "\n%*s}", level, ""); } } diff --git a/gdb/testsuite/gdb.base/call-sc.exp b/gdb/testsuite/gdb.base/call-sc.exp index c799a710867..3f99832f4df 100644 --- a/gdb/testsuite/gdb.base/call-sc.exp +++ b/gdb/testsuite/gdb.base/call-sc.exp @@ -73,8 +73,17 @@ proc start_scalars_test { type } { set foo_t "$expect_out(1,string)" pass "$test (${foo_t})" } + -re "type = enum .*\\{\r\n.*\r\n\\}\r\n$gdb_prompt $" { + set foo_t "$expect_out(1,string)" + pass "$test (${foo_t})" + } + } + gdb_test_multiple "ptype/r foo" "ptype foo; ${testfile}" { + -re "type = .*$gdb_prompt $" { + set foo_t "$expect_out(1,string)" + pass "$test (${foo_t})" + } } - gdb_test "ptype/r foo" "type = ${foo_t}" "ptype foo; ${testfile} $expect_out(1,string)" } diff --git a/gdb/testsuite/gdb.base/ctf-ptype.exp b/gdb/testsuite/gdb.base/ctf-ptype.exp index d17ce3bbc91..e6bae63562b 100644 --- a/gdb/testsuite/gdb.base/ctf-ptype.exp +++ b/gdb/testsuite/gdb.base/ctf-ptype.exp @@ -42,13 +42,27 @@ gdb_load $binfile # with stabs compilers which fail to use a nameless stab (such as # pre-2.4.5 versions of gcc and most non-gcc compilers). + +set re1 [multi_line \ + "type = enum primary1_tag {" \ + " red1 = 0," \ + " green1 = 1," \ + " blue1 = 2" \ + "}.*$gdb_prompt $"] +set re2 [multi_line \ + "type = enum {" \ + " red1 = 0," \ + " green1 = 1," \ + " blue1 = 2" \ + "}.*$gdb_prompt $"] + gdb_test_multiple "ptype red1" "ptype unnamed enumeration member" { - -re "type = enum primary1_tag \{red1, green1, blue1\}.*$gdb_prompt $" { + -re $re1 { # The workaround is in effect. As this is a compiler, not GDB, # bug, we'll make it a PASS but perhaps it should be an XFAIL. pass "ptype unnamed enumeration member (worked around)" } - -re "type = enum \{red1, green1, blue1\}.*$gdb_prompt $" { + -re $re2 { pass "ptype unnamed enumeration member" } } @@ -112,15 +126,32 @@ gdb_test "ptype union t_union" \ # test ptype command with enums # -gdb_test "ptype primary" "type = enum .red, green, blue.*" "ptype unnamed enumeration" +gdb_test "ptype primary" \ + [multi_line \ + "type = enum {" \ + " red = 0," \ + " green = 1," \ + " blue = 2" \ + "}"] "ptype unnamed enumeration" -gdb_test "ptype enum colors" "type = enum colors \{yellow, purple, pink\}.*" "ptype named enumeration" +gdb_test "ptype enum colors" \ + [multi_line \ + "type = enum colors {" \ + " yellow = 0," \ + " purple = 1," \ + " pink = 2" \ + "}"] "ptype named enumeration" # # test ptype command with enums as typedef # -gdb_test "ptype boolean" "type = enum (boolean |)\{FALSE, TRUE\}.*" "ptype unnamed typedef'd enumeration" +gdb_test "ptype boolean" \ + [multi_line \ + "type = enum (boolean |){" \ + " FALSE = 0," \ + " TRUE = 1" \ + "}"] "ptype unnamed typedef'd enumeration" gdb_test "list -q main" ".*" @@ -133,19 +164,44 @@ gdb_test "ptype t_union3" "type = union (t_union3 |)\{.* *double v_double_member;.* *int v_int_member;.*\}" "printing typedef'd union" -gdb_test "ptype enum bvals" "type = enum bvals \{my_false, my_true\}.*" "ptype named typedef'd enumf'd enum" +gdb_test "ptype enum bvals" \ + [multi_line \ + "type = enum bvals {" \ + " my_false = 0," \ + " my_true = 1" \ + "}"] "ptype named typedef'd enumf'd enum" # # test ptype command with out-of-order enum values # -gdb_test "ptype enum misordered" "type = enum misordered \{two = 2, one = 1, zero = 0, three = 3\}.*" "ptype misordered enumeration" +gdb_test "ptype enum misordered" \ + [multi_line \ + "type = enum misordered {" \ + " two = 2," \ + " one = 1," \ + " zero = 0," \ + " three = 3" \ + "}"] "ptype misordered enumeration" # # test ptype command with a named enum's value # -gdb_test "ptype three" "type = enum misordered \{two = 2, one = 1, zero = 0, three = 3\}.*" "ptype named enumeration member" - -gdb_test "ptype red" "type = enum \{red, green, blue\}.*" "ptype unnamed enumeration member #2" +gdb_test "ptype three" \ + [multi_line \ + "type = enum misordered {" \ + " two = 2," \ + " one = 1," \ + " zero = 0," \ + " three = 3" \ + "}"] "ptype named enumeration member" + +gdb_test "ptype red" \ + [multi_line \ + "type = enum {" \ + " red = 0," \ + " green = 1," \ + " blue = 2" \ + "}"] "ptype unnamed enumeration member #2" # # test ptype command with arrays diff --git a/gdb/testsuite/gdb.base/ptype.exp b/gdb/testsuite/gdb.base/ptype.exp index 8545c716f27..5602defba64 100644 --- a/gdb/testsuite/gdb.base/ptype.exp +++ b/gdb/testsuite/gdb.base/ptype.exp @@ -34,13 +34,27 @@ set gcc_compiled [is_c_compiler_gcc] # with stabs compilers which fail to use a nameless stab (such as # pre-2.4.5 versions of gcc and most non-gcc compilers). + +set re1 [multi_line \ + "type = enum primary1_tag {" \ + " red1 = 0," \ + " green1 = 1," \ + " blue1 = 2" \ + "}.*$gdb_prompt $"] +set re2 [multi_line \ + "type = enum {" \ + " red1 = 0," \ + " green1 = 1," \ + " blue1 = 2" \ + "}.*$gdb_prompt $"] + gdb_test_multiple "ptype red1" "ptype unnamed enumeration member" { - -re "type = enum primary1_tag \{red1, green1, blue1\}.*$gdb_prompt $" { + -re $re1 { # The workaround is in effect. As this is a compiler, not GDB, # bug, we'll make it a PASS but perhaps it should be an XFAIL. pass "ptype unnamed enumeration member (worked around)" } - -re "type = enum \{red1, green1, blue1\}.*$gdb_prompt $" { + -re $re2 { pass "ptype unnamed enumeration member" } } @@ -118,15 +132,32 @@ gdb_test "ptype union tu_link" \ # test ptype command with enums # -gdb_test "ptype primary" "type = enum .red, green, blue.*" "ptype unnamed enumeration" +gdb_test "ptype primary" \ + [multi_line \ + "type = enum {" \ + " red = 0," \ + " green = 1," \ + " blue = 2" \ + "}"] "ptype unnamed enumeration" -gdb_test "ptype enum colors" "type = enum colors \{yellow, purple, pink\}.*" "ptype named enumeration" +gdb_test "ptype enum colors" \ + [multi_line \ + "type = enum colors {" \ + " yellow = 0," \ + " purple = 1," \ + " pink = 2" \ + "}"] "ptype named enumeration" # # test ptype command with enums as typedef # -gdb_test "ptype boolean" "type = enum (boolean |)\{FALSE, TRUE\}.*" "ptype unnamed typedef'd enumeration" +gdb_test "ptype boolean" \ + [multi_line \ + "type = enum (boolean |){" \ + " FALSE = 0," \ + " TRUE = 1" \ + "}"] "ptype unnamed typedef'd enumeration" # And check that whatis shows the name, not "enum {...}". # This probably fails for all DWARF 1 cases, so assume so for now. -fnf @@ -157,19 +188,44 @@ gdb_test "ptype t_union3" "type = union (t_union3 |)\{.* *double v_double_member;.* *int v_int_member;.*\}" "printing typedef'd union" -gdb_test "ptype enum bvals" "type = enum bvals \{my_false, my_true\}.*" "ptype named typedef'd enumf'd enum" +gdb_test "ptype enum bvals" \ + [multi_line \ + "type = enum bvals {" \ + " my_false = 0," \ + " my_true = 1" \ + "}"] "ptype named typedef'd enumf'd enum" # # test ptype command with out-of-order enum values # -gdb_test "ptype enum misordered" "type = enum misordered \{two = 2, one = 1, zero = 0, three = 3\}.*" "ptype misordered enumeration" +gdb_test "ptype enum misordered" \ + [multi_line \ + "type = enum misordered {" \ + " two = 2," \ + " one = 1," \ + " zero = 0," \ + " three = 3" \ + "}"] "ptype misordered enumeration" # # test ptype command with a named enum's value # -gdb_test "ptype three" "type = enum misordered \{two = 2, one = 1, zero = 0, three = 3\}.*" "ptype named enumeration member" - -gdb_test "ptype red" "type = enum \{red, green, blue\}.*" "ptype unnamed enumeration member #2" +gdb_test "ptype three" \ + [multi_line \ + "type = enum misordered {" \ + " two = 2," \ + " one = 1," \ + " zero = 0," \ + " three = 3" \ + "}"] "ptype named enumeration member" + +gdb_test "ptype red" \ + [multi_line \ + "type = enum {" \ + " red = 0," \ + " green = 1," \ + " blue = 2" \ + "}"] "ptype unnamed enumeration member #2" # # test ptype command with basic C types diff --git a/gdb/testsuite/gdb.base/whatis-ptype-typedefs.exp b/gdb/testsuite/gdb.base/whatis-ptype-typedefs.exp index 134e3f81fd6..98f2f6a9b3d 100644 --- a/gdb/testsuite/gdb.base/whatis-ptype-typedefs.exp +++ b/gdb/testsuite/gdb.base/whatis-ptype-typedefs.exp @@ -103,10 +103,10 @@ set table { {"v_long_double_typedef" "long_double_typedef" "long double"} {"v_long_double_typedef2" "long_double_typedef2" "long double"} - {"colors_typedef" "(enum )?colors" "enum colors( : unsigned int)? {red, green, blue}"} - {"colors_typedef2" "colors_typedef" "enum colors( : unsigned int)? {red, green, blue}"} - {"v_colors_typedef" "colors_typedef" "enum colors( : unsigned int)? {red, green, blue}"} - {"v_colors_typedef2" "colors_typedef2" "enum colors( : unsigned int)? {red, green, blue}"} + {"colors_typedef" "(enum )?colors" "enum colors( : unsigned int)? {.*red = 0,.* green = 1,.* blue = 2.*}"} + {"colors_typedef2" "colors_typedef" "enum colors( : unsigned int)? {.*red = 0,.* green = 1,.* blue = 2.*}"} + {"v_colors_typedef" "colors_typedef" "enum colors( : unsigned int)? {.*red = 0,.* green = 1,.* blue = 2.*}"} + {"v_colors_typedef2" "colors_typedef2" "enum colors( : unsigned int)? {.*red = 0,.* green = 1,.* blue = 2.*}"} {"func_ftype" "void \\(void\\)" "void \\(void\\)"} {"func_ftype2" "func_ftype" "void \\(void\\)"} diff --git a/gdb/testsuite/gdb.cp/classes.exp b/gdb/testsuite/gdb.cp/classes.exp index 033c913b0ec..231c6c7cff2 100644 --- a/gdb/testsuite/gdb.cp/classes.exp +++ b/gdb/testsuite/gdb.cp/classes.exp @@ -510,16 +510,37 @@ proc test_enums {} { # ptype on the enum member + set re1 [multi_line \ + "type = enum ClassWithEnum::PrivEnum (: unsigned (int|short|char) )?{" \ + " ?(ClassWithEnum::)?red = 0," \ + " (ClassWithEnum::)?green = 1," \ + " (ClassWithEnum::)?blue = 2," \ + " (ClassWithEnum::)?yellow = 42" \ + "?}$nl$gdb_prompt $"] + set re2 [multi_line \ + "type = enum PrivEnum {" \ + " ?(ClassWithEnum::)?red = 0," \ + " (ClassWithEnum::)?green = 1," \ + " (ClassWithEnum::)?blue = 2," \ + " (ClassWithEnum::)?yellow = 42" \ + "?}$nl$gdb_prompt $"] + set re3 [multi_line \ + "type = enum {" \ + " ?red = 0," \ + " green = 1," \ + " blue = 2," \ + " yellow = 42" \ + "?}$nl$gdb_prompt $"] gdb_test_multiple "ptype obj_with_enum.priv_enum" "ptype obj_with_enum.priv_enum" { - -re "type = enum ClassWithEnum::PrivEnum (: unsigned (int|short|char) )?\{ ?(ClassWithEnum::)?red, (ClassWithEnum::)?green, (ClassWithEnum::)?blue, (ClassWithEnum::)?yellow = 42 ?\}$nl$gdb_prompt $" { + -re $re1 { pass "ptype obj_with_enum.priv_enum" } - -re "type = enum PrivEnum \{ ?(ClassWithEnum::)?red, (ClassWithEnum::)?green, (ClassWithEnum::)?blue, (ClassWithEnum::)?yellow = 42 ?\}$nl$gdb_prompt $" { + -re $re2 { # gcc 2.95.3 -gdwarf-2 # gcc 3.3.2 -gdwarf-2 pass "ptype obj_with_enum.priv_enum" } - -re "type = enum \{ ?red, green, blue, yellow = 42 ?\}$nl$gdb_prompt $" { + -re $re3 { # This case case is a little dubious, but it's not clear what # ought to be required of a ptype on a private enum... # -sts 19990324 diff --git a/gdb/testsuite/gdb.cp/empty-enum.exp b/gdb/testsuite/gdb.cp/empty-enum.exp index a74e3aae288..437dd0ad086 100644 --- a/gdb/testsuite/gdb.cp/empty-enum.exp +++ b/gdb/testsuite/gdb.cp/empty-enum.exp @@ -53,11 +53,19 @@ gdb_test "print arg2" " = 4" # Xfail for missing DW_AT_type in DW_TAG_enumeration_type, gcc PR debug/16063. set have_xfail [expr {[test_compiler_info gcc-*] && [gcc_major_version] < 5}] +set enum1 [multi_line "" \ + "type = enum enum1 : unsigned int {" \ + " " \ + "}"] +set enum1_no_type [multi_line "" \ + "type = enum enum1 {" \ + " " \ + "}"] gdb_test_multiple "ptype arg1" "" { - -re -wrap "type = enum enum1 : unsigned int \\{\\}" { + -re -wrap $enum1 { pass $gdb_test_name } - -re -wrap "type = enum enum1 \\{\\}" { + -re -wrap $enum1_no_type { if { $have_xfail } { setup_xfail *-*-* gcc/16063 } @@ -65,11 +73,19 @@ gdb_test_multiple "ptype arg1" "" { } } +set enum2 [multi_line "" \ + "type = enum class enum2 : unsigned char {" \ + " " \ + "}"] +set enum2_no_type [multi_line "" \ + "type = enum class enum2 {" \ + " " \ + "}"] gdb_test_multiple "ptype arg2" "" { - -re -wrap "type = enum class enum2 : unsigned char \\{\\}" { + -re -wrap $enum2 { pass $gdb_test_name } - -re -wrap "type = enum class enum2 \\{\\}" { + -re -wrap $enum2_no_type { if { $have_xfail } { setup_xfail *-*-* gcc/16063 } diff --git a/gdb/testsuite/gdb.cp/enum-class.exp b/gdb/testsuite/gdb.cp/enum-class.exp index 50431d19c00..b7858719f1e 100644 --- a/gdb/testsuite/gdb.cp/enum-class.exp +++ b/gdb/testsuite/gdb.cp/enum-class.exp @@ -29,7 +29,11 @@ if {![runto_main]} { } gdb_test "ptype E1" \ - "type = enum class E1 (: int )?{E1::HI = 7, E1::THERE}" + [multi_line \ + "type = enum class E1 (: int )?{" \ + " E1::HI = 7," \ + " E1::THERE = 8" \ + "}"] gdb_test "print E1::HI" " = E1::HI" gdb_test "print (int) E1::HI" " = 7" diff --git a/gdb/testsuite/gdb.cp/nested-types.exp b/gdb/testsuite/gdb.cp/nested-types.exp index 5c71bbc08ca..e0a9d163582 100644 --- a/gdb/testsuite/gdb.cp/nested-types.exp +++ b/gdb/testsuite/gdb.cp/nested-types.exp @@ -154,9 +154,9 @@ proc make_enum {result_var id parent_list indent_lvl log} { upvar $result_var result set s "[qual_name E$id $parent_list]" - set a "[qual_name A$id $parent_list]" - set b "[qual_name B$id $parent_list]" - set c "[qual_name C$id $parent_list]" + set a "[qual_name A$id $parent_list] = 0" + set b "[qual_name B$id $parent_list] = 1" + set c "[qual_name C$id $parent_list] = 2" lappend result [list "type" "public" "enum" $s [list $a $b $c]] if {$log} { diff --git a/gdb/testsuite/gdb.dwarf2/enum-type.exp b/gdb/testsuite/gdb.dwarf2/enum-type.exp index 68549e7fb1c..7a37608670e 100644 --- a/gdb/testsuite/gdb.dwarf2/enum-type.exp +++ b/gdb/testsuite/gdb.dwarf2/enum-type.exp @@ -109,11 +109,17 @@ if { [prepare_for_testing "failed to prepare" ${testfile} \ gdb_test "print sizeof(enum E)" " = 4" -gdb_test "ptype enum EU" "type = enum EU {TWO = 2}" \ - "ptype EU in enum C" +gdb_test "ptype enum EU" \ + [multi_line \ + "type = enum EU {" \ + " TWO = 2" \ + "}"] "ptype EU in enum C" gdb_test_no_output "set lang c++" -gdb_test "ptype enum EU" "type = enum EU : unsigned int {TWO = 2}" \ - "ptype EU in C++" +gdb_test "ptype enum EU" \ + [multi_line \ + "type = enum EU : unsigned int {" \ + " TWO = 2" \ + "}"] "ptype EU in C++" gdb_test "p ns::val1" \ " = ns::val1" diff --git a/gdb/testsuite/gdb.xml/tdesc-regs.exp b/gdb/testsuite/gdb.xml/tdesc-regs.exp index 25028fbd699..96f0fe089aa 100644 --- a/gdb/testsuite/gdb.xml/tdesc-regs.exp +++ b/gdb/testsuite/gdb.xml/tdesc-regs.exp @@ -212,7 +212,12 @@ gdb_test "ptype \$mixed_flags" \ " bool C @4;" \ " uint32_t D @5;" \ " uint32_t @6-7;" \ - " enum Z_values {yes = 1, no = 0, maybe = 2, so} Z @8-9;" \ + " enum Z_values {" \ + " yes = 1," \ + " no = 0," \ + " maybe = 2," \ + " so = 3" \ + " } Z @8-9;" \ "}"] # Reggroups should have at least general and the extra foo group gdb_test "maintenance print reggroups" \ diff --git a/gdb/testsuite/lib/cp-support.exp b/gdb/testsuite/lib/cp-support.exp index a64194f18f6..cf26406260d 100644 --- a/gdb/testsuite/lib/cp-support.exp +++ b/gdb/testsuite/lib/cp-support.exp @@ -582,23 +582,44 @@ proc cp_test_ptype_class { in_exp in_testname in_key in_tag in_class_table switch $nested_key { enum { - set expected_result \ - "enum $nested_name (: (unsigned )?int )?\{" - foreach c $nested_children { - append expected_result "$c, " - } - set expected_result \ - [string trimright $expected_result { ,}] - append expected_result "\};" + set expected_result "enum $nested_name : unsigned int \{" cp_ptype_class_verbose \ "Expecting enum result: $expected_result" - if {![regexp -- $expected_result $actual_line]} { + if {![string equal $expected_result $actual_line]} { set txt "$in_testname // wrong nested type enum" append txt " definition: $actual_line" fail $txt queue delete $line_queue return false } + # This will be followed by lines for each value of the + # enum. + cp_ptype_class_verbose "matched enum value" + foreach m $nested_children { + set actual_line \ + [cp_support_internal::next_line $line_queue] + cp_ptype_class_verbose "Expecting enum value: $m" + # Remove the trailing comma from the actual line to + # simplify comparison. + set trimmed_actual_line \ + [string trimright $actual_line " ,"] + if {![string equal $m $trimmed_actual_line]} { + set txt "$in_testname // unexpected enum value: " + append txt $m + fail $txt + queue delete $line_queue + return false + } + cp_ptype_class_verbose "matched enum value \"$m\"" + } + + # Nested enum values always end with a trailing curly brace. + set actual_line [cp_support_internal::next_line $line_queue] + if {![string equal $actual_line "\};"]} { + fail "$in_testname // missing closing curly brace" + queue delete $line_queue + return false + } cp_ptype_class_verbose "passed enum $nested_name" }