[3/3,v5] gdb: Make printing enum types prettier.

Message ID 20251224100514.36500-4-daniel.knezevic@htecgroup.com
State New
Headers
Series gdb: Make printing enum types prettier |

Commit Message

Daniel Knezevic Dec. 24, 2025, 10:05 a.m. UTC
  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 "<no enum values>"
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 {
    <no enum values>
}

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=19294
---
 gdb/c-typeprint.c                             | 28 +++----
 gdb/testsuite/gdb.base/call-sc.exp            | 11 ++-
 gdb/testsuite/gdb.base/ctf-ptype.exp          | 76 ++++++++++++++++---
 gdb/testsuite/gdb.base/ptype.exp              | 76 ++++++++++++++++---
 .../gdb.base/whatis-ptype-typedefs.exp        |  8 +-
 gdb/testsuite/gdb.cp/classes.exp              | 27 ++++++-
 gdb/testsuite/gdb.cp/empty-enum.exp           | 24 +++++-
 gdb/testsuite/gdb.cp/enum-class.exp           |  6 +-
 gdb/testsuite/gdb.cp/nested-types.exp         |  6 +-
 gdb/testsuite/gdb.dwarf2/enum-type.exp        | 14 +++-
 gdb/testsuite/gdb.xml/tdesc-regs.exp          |  7 +-
 gdb/testsuite/lib/cp-support.exp              | 39 +++++++---
 12 files changed, 258 insertions(+), 64 deletions(-)
  

Comments

Tom Tromey Jan. 6, 2026, 8:53 p.m. UTC | #1
>>>>> "Daniel" == Daniel Knezevic <daniel.knezevic@htecgroup.com> writes:

Daniel> +      if (len == 0)
Daniel>  	{
Daniel> -	  QUIT;
Daniel> -	  if (i)
Daniel> -	    gdb_printf (stream, ", ");
Daniel> -	  stream->wrap_here (4);
Daniel> -	  fputs_styled (type->field (i).name (),
Daniel> -			variable_name_style.style (), stream);
Daniel> -	  if (lastval != type->field (i).loc_enumval ())
Daniel> +	  fprintf_styled (stream, metadata_style.style (),
Daniel> +			  "%*s<no enum values>", level + 4, "");
Daniel> +	}

The braces aren't needed here, since there's only one statement.

Also I think it would be better not to use fprintf_styled, since I think
that will style the leading spaces as well.  Instead something like:

gdb_printf (stream, "%*s%ps", level + 4, "", styled_string (...))

would be better.

Daniel> +	      fprintf_styled (stream, variable_name_style.style (),
Daniel> +			      "%*s%s", level + 4, "", type->field (i).name ());

Same sort of thing here.

Tom
  

Patch

diff --git a/gdb/c-typeprint.c b/gdb/c-typeprint.c
index 134cbdafcf2..7988473cdfd 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)
 	{
-	  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 ())
+	  fprintf_styled (stream, metadata_style.style (),
+			  "%*s<no enum values>", level + 4, "");
+	}
+      else
+	{
+	  for (int i = 0; i < len; i++)
 	    {
+	      QUIT;
+	      if (i != 0)
+		gdb_printf (stream, ",\n");
+	      fprintf_styled (stream, variable_name_style.style (),
+			      "%*s%s", level + 4, "", 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 5f7cfe00c03..6f46ff2a67c 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 3896457212d..dd730c26374 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 694caa70039..bf9ecf3e15e 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 744c6ee999e..8eec4470618 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 64689c701e2..13195a08e4f 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 487d5619e44..0781e486706 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 {" \
+    "    <no enum values>" \
+    "}"]
+set enum1_no_type [multi_line "" \
+    "type = enum enum1 {" \
+    "    <no enum values>" \
+    "}"]
 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 {" \
+    "    <no enum values>" \
+    "}"]
+set enum2_no_type [multi_line "" \
+    "type = enum class enum2 {" \
+    "    <no enum values>" \
+    "}"]
 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 dead0c51511..fbfda8937f0 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 28ce8e0c094..0a4939e5062 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 f077c0acb2a..6f751406f9b 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 63f5ebc90d8..2dcff9ab468 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 ded4cf8aeb6..5795836ce04 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"
 		}