Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Daniel Knezevic <daniel.knezevic@htecgroup.com>
To: "gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Cc: Tom Tromey <tom@tromey.com>, Pedro Alves <pedro@palves.net>,
	Simon Marchi <simark@simark.ca>,
	Daniel Knezevic <daniel.knezevic@htecgroup.com>
Subject: [PATCH 1/3] gdb: Move logic for printing enums to a helper function
Date: Wed, 26 Nov 2025 12:02:58 +0000	[thread overview]
Message-ID: <20251126120210.919813-2-daniel.knezevic@htecgroup.com> (raw)
In-Reply-To: <20251126120210.919813-1-daniel.knezevic@htecgroup.com>

---
 gdb/c-typeprint.c | 151 +++++++++++++++++++++++++---------------------
 1 file changed, 81 insertions(+), 70 deletions(-)

diff --git a/gdb/c-typeprint.c b/gdb/c-typeprint.c
index 1dab82135d0..9e39c3d9b77 100644
--- a/gdb/c-typeprint.c
+++ b/gdb/c-typeprint.c
@@ -1292,6 +1292,83 @@ c_type_print_base_struct_union (struct type *type, struct ui_file *stream,
     }
 }
 
+/* Helper for 'c_type_print_base' that handles enums.
+   For a description of the arguments, see 'c_type_print_base'.  */
+
+static void
+c_type_print_base_enum (struct type *type, struct ui_file *stream,
+			int show, int level,
+			enum language language,
+			const struct type_print_options *flags,
+			struct print_offset_data *podata)
+{
+  c_type_print_modifier (type, stream, 0, 1, language);
+  gdb_printf (stream, "enum ");
+  if (type->is_declared_class ())
+    gdb_printf (stream, "class ");
+  /* Print the tag name if it exists.
+     The aCC compiler emits a spurious
+     "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
+     tag for unnamed struct/union/enum's, which we don't
+     want to print.  */
+  if (type->name () != NULL
+      && !startswith (type->name (), "{unnamed"))
+    {
+      print_name_maybe_canonical (type->name (), flags, stream);
+      if (show > 0)
+	gdb_puts (" ", stream);
+    }
+
+  stream->wrap_here (4);
+  if (show < 0)
+    {
+      /* If we just printed a tag name, no need to print anything
+	 else.  */
+      if (type->name () == NULL)
+	gdb_printf (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
+	 at all).  We choose to print the underlying type, if it
+	 has a name, when in C++ on the theory that it's better to
+	 print too much than too little; but conversely not to
+	 print something egregiously outside the current
+	 language's syntax.  */
+      if (language == language_cplus && type->target_type () != NULL)
+	{
+	  struct type *underlying = check_typedef (type->target_type ());
+
+	  if (underlying->name () != NULL)
+	    gdb_printf (stream, ": %s ", underlying->name ());
+	}
+
+      gdb_printf (stream, "{");
+      int len = type->num_fields ();
+      for (int i = 0; i < len; i++)
+	{
+	  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 ())
+	    {
+	      gdb_printf (stream, " = %s",
+			  plongest (type->field (i).loc_enumval ()));
+	      lastval = type->field (i).loc_enumval ();
+	    }
+	  lastval++;
+	}
+      gdb_printf (stream, "}");
+    }
+}
+
 /* Print the name of the type (or the ultimate pointer target,
    function value or array element), or the description of a structure
    or union.
@@ -1317,9 +1394,6 @@ c_type_print_base_1 (struct type *type, struct ui_file *stream,
 		     const struct type_print_options *flags,
 		     struct print_offset_data *podata)
 {
-  int i;
-  int len;
-
   QUIT;
 
   if (type == NULL)
@@ -1399,71 +1473,8 @@ c_type_print_base_1 (struct type *type, struct ui_file *stream,
       break;
 
     case TYPE_CODE_ENUM:
-      c_type_print_modifier (type, stream, 0, 1, language);
-      gdb_printf (stream, "enum ");
-      if (type->is_declared_class ())
-	gdb_printf (stream, "class ");
-      /* Print the tag name if it exists.
-	 The aCC compiler emits a spurious
-	 "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
-	 tag for unnamed struct/union/enum's, which we don't
-	 want to print.  */
-      if (type->name () != NULL
-	  && !startswith (type->name (), "{unnamed"))
-	{
-	  print_name_maybe_canonical (type->name (), flags, stream);
-	  if (show > 0)
-	    gdb_puts (" ", stream);
-	}
-
-      stream->wrap_here (4);
-      if (show < 0)
-	{
-	  /* If we just printed a tag name, no need to print anything
-	     else.  */
-	  if (type->name () == NULL)
-	    gdb_printf (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
-	     at all).  We choose to print the underlying type, if it
-	     has a name, when in C++ on the theory that it's better to
-	     print too much than too little; but conversely not to
-	     print something egregiously outside the current
-	     language's syntax.  */
-	  if (language == language_cplus && type->target_type () != NULL)
-	    {
-	      struct type *underlying = check_typedef (type->target_type ());
-
-	      if (underlying->name () != NULL)
-		gdb_printf (stream, ": %s ", underlying->name ());
-	    }
-
-	  gdb_printf (stream, "{");
-	  len = type->num_fields ();
-	  for (i = 0; i < len; i++)
-	    {
-	      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 ())
-		{
-		  gdb_printf (stream, " = %s",
-			      plongest (type->field (i).loc_enumval ()));
-		  lastval = type->field (i).loc_enumval ();
-		}
-	      lastval++;
-	    }
-	  gdb_printf (stream, "}");
-	}
+      c_type_print_base_enum (type, stream, show, level,
+			      language, flags, podata);
       break;
 
     case TYPE_CODE_FLAGS:
@@ -1492,8 +1503,8 @@ c_type_print_base_1 (struct type *type, struct ui_file *stream,
 			      level + 4, "",
 			      metadata_style.style ().ptr (), nullptr);
 	      }
-	    len = type->num_fields ();
-	    for (i = 0; i < len; i++)
+	    int len = type->num_fields ();
+	    for (int i = 0; i < len; i++)
 	      {
 		QUIT;
 		print_spaces (level + 4, stream);
-- 
2.43.0

  reply	other threads:[~2025-11-26 12:03 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-26 12:02 [PATCH 0/3 v4] gdb: Make printing enum types prettier Daniel Knezevic
2025-11-26 12:02 ` Daniel Knezevic [this message]
2025-11-26 12:03 ` [PATCH 2/3] gdb: Replace \r\n with multi_line to make tests more readable Daniel Knezevic
2025-11-26 12:43   ` Andreas Schwab
2025-11-26 12:03 ` [PATCH 3/3] gdb: Make printing enum types prettier Daniel Knezevic
2025-12-01 20:23 ` [PATCH 0/3 v4] " Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251126120210.919813-2-daniel.knezevic@htecgroup.com \
    --to=daniel.knezevic@htecgroup.com \
    --cc=gdb-patches@sourceware.org \
    --cc=pedro@palves.net \
    --cc=simark@simark.ca \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox