Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH 2/2] gdb: don't print trailing space after base classes in pretty format
Date: Mon, 17 Aug 2026 10:59:02 -0400	[thread overview]
Message-ID: <20260817145907.142300-2-simon.marchi@efficios.com> (raw)
In-Reply-To: <20260817145907.142300-1-simon.marchi@efficios.com>

After working on the previous patch, the formatting of the values
printed in test gdb.cp/anon-struct-with-base.exp with "print pretty on"
looks wrong to me.  I think that with "print pretty on", the "<No data
fields>" string should be on its own line.  The closing curly brace
should also be on its own line.

As an exception, when a type has absolutely nothing in it, we print
this, which seem reasonable:

    <base_no_data> = {<No data fields>},

Concretely, this patch changes this:

    $ ./gdb -nx -q --data-directory=data-directory testsuite/outputs/gdb.cp/anon-struct-with-base/anon-struct-with-base -ex "with print pretty -- p v_no_data_base_data" -ex "with print pretty -- p v_no_data_base_no_data" -batch
    $1 = {
      <base_data> = {
        a = 3
      }, <No data fields>}
    $2 = {
      <base_no_data> = {<No data fields>}, <No data fields>}

to this:

    $ ./gdb -nx -q --data-directory=data-directory testsuite/outputs/gdb.cp/anon-struct-with-base/anon-struct-with-base -ex "with print pretty -- p v_no_data_base_data" -ex "with print pretty -- p v_no_data_base_no_data" -batch
    $1 = {
      <base_data> = {
        a = 3
      },
      <No data fields>
    }
    $2 = {
      <base_no_data> = {<No data fields>},
      <No data fields>
    }

Finally, this change also gets rid of some unnecessary trailing spaces
printed after base classes.  This is not really visible for users, but
it is visible in the test changes.

Change-Id: Ia1c28c8bf2c6a8442c2cb610e06058e4dac030b9
---
 gdb/cp-valprint.c                             | 22 +++++++++++++++++--
 .../gdb.cp/anon-struct-with-base.exp          | 12 ++++++----
 2 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/gdb/cp-valprint.c b/gdb/cp-valprint.c
index d450e90e1e1d..bd9544307127 100644
--- a/gdb/cp-valprint.c
+++ b/gdb/cp-valprint.c
@@ -165,7 +165,21 @@ cp_print_value_fields (struct value *val, struct ui_file *stream,
 
   /* If there are no data fields, skip this part */
   if (len == n_baseclasses || !len)
-    fprintf_styled (stream, metadata_style.style (), "<No data fields>");
+    {
+      if (options->prettyformat && n_baseclasses > 0)
+	{
+	  gdb_printf (stream, "\n");
+	  print_spaces (2 + 2 * recurse, stream);
+	}
+
+      fprintf_styled (stream, metadata_style.style (), "<No data fields>");
+
+      if (options->prettyformat && n_baseclasses > 0)
+	{
+	  gdb_printf (stream, "\n");
+	  print_spaces (2 * recurse, stream);
+	}
+    }
   else
     {
       size_t statmem_obstack_initial_size = 0;
@@ -532,7 +546,11 @@ cp_print_value (struct value *val, struct ui_file *stream,
 		 0);
 	    }
 	}
-      gdb_puts (", ", stream);
+
+      gdb_puts (",", stream);
+
+      if (!options->prettyformat)
+	gdb_puts (" ", stream);
 
     flush_it:
       ;
diff --git a/gdb/testsuite/gdb.cp/anon-struct-with-base.exp b/gdb/testsuite/gdb.cp/anon-struct-with-base.exp
index 2ff7036198a2..1b9cf9f19020 100644
--- a/gdb/testsuite/gdb.cp/anon-struct-with-base.exp
+++ b/gdb/testsuite/gdb.cp/anon-struct-with-base.exp
@@ -39,7 +39,7 @@ gdb_test "with print pretty on -- print v_data_base_data" \
 	 "$::valnum_re = \{" \
 	 "  <base_data> = \{" \
 	 "    a = 1" \
-	 "  \}, " \
+	 "  \}," \
 	 "  members of <unnamed type>:" \
 	 "  b = 2" \
 	 "\}"]
@@ -49,12 +49,14 @@ gdb_test "with print pretty on -- print v_no_data_base_data" \
 	 "$::valnum_re = \{" \
 	 "  <base_data> = \{" \
 	 "    a = 3" \
-	 "  \}, <No data fields>\}"]
+	 "  \}," \
+	 "  <No data fields>" \
+	 "\}"]
 
 gdb_test "with print pretty on -- print v_data_base_no_data" \
     [multi_line \
 	 "$::valnum_re = \{" \
-	 "  <base_no_data> = \{<No data fields>\}, " \
+	 "  <base_no_data> = \{<No data fields>\}," \
 	 "  members of <unnamed type>:" \
 	 "  c = 4" \
 	 "\}"]
@@ -62,4 +64,6 @@ gdb_test "with print pretty on -- print v_data_base_no_data" \
 gdb_test "with print pretty on -- print v_no_data_base_no_data" \
     [multi_line \
 	 "$::valnum_re = \{" \
-	 "  <base_no_data> = \{<No data fields>\}, <No data fields>\}"]
+	 "  <base_no_data> = \{<No data fields>\}," \
+	 "  <No data fields>" \
+	 "\}"]
-- 
2.55.0


  reply	other threads:[~2026-08-17 14:59 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-17 14:59 [PATCH 1/2] gdb: fix crash when pretty printing anonymous struct with base class Simon Marchi
2026-08-17 14:59 ` Simon Marchi [this message]
2026-08-18  5:21 ` Kevin Buettner
2026-08-20 14:12   ` Simon Marchi
2026-08-20 16:21 ` Tom Tromey
2026-08-20 16:26   ` Simon Marchi

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=20260817145907.142300-2-simon.marchi@efficios.com \
    --to=simon.marchi@efficios.com \
    --cc=gdb-patches@sourceware.org \
    /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