[binutils-gdb] gdb: don't print trailing space after base classes in pretty format

Simon Marchi via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=d2102b0d6a95ba3b37204976fb3f6ebd935822fb

commit d2102b0d6a95ba3b37204976fb3f6ebd935822fb
Author: Simon Marchi <[email protected]>
Date:   Mon Aug 17 10:59:02 2026 -0400

    gdb: don't print trailing space after base classes in pretty format
    
    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
    Approved-By: Kevin Buettner <[email protected]>

Diff:
---
 gdb/cp-valprint.c                              | 22 ++++++++++++++++++++--
 gdb/testsuite/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 d450e90e1e1..bd954430712 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 2ff7036198a..1b9cf9f1902 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>" \
+	 "\}"]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.