[v3,2/8] Change generic_emit_char to print the quotes

Message ID 20251228-string-printing-2-v3-2-ec5cc9dd0c71@tromey.com
State New
Headers
Series Refactor character printing |

Commit Message

Tom Tromey Dec. 28, 2025, 8:13 p.m. UTC
  All callers of generic_emit_char print the quotes around the
character, then pass the quote character to the function.  It seemed
better to just have generic_emit_char print the quotes itself.
---
 gdb/c-lang.c    |  2 --
 gdb/f-lang.h    |  2 --
 gdb/rust-lang.c | 11 +++++++----
 gdb/valprint.c  |  2 ++
 4 files changed, 9 insertions(+), 8 deletions(-)
  

Comments

Simon Marchi Jan. 7, 2026, 8:31 p.m. UTC | #1
On 2025-12-28 15:13, Tom Tromey wrote:
> All callers of generic_emit_char print the quotes around the
> character, then pass the quote character to the function.  It seemed
> better to just have generic_emit_char print the quotes itself.

I don't know if this is affected by the rest of the series, but
generic_emit_char always receives a single quote as the quoter, so that
parameter could be removed.  The function could still use a variable to
keep the "quoter" name, like:

    constexpr int quoter = '\'';

Still, this patch LGTM.

Approved-By: Simon Marchi <simon.marchi@efficios.com>

Simon
  

Patch

diff --git a/gdb/c-lang.c b/gdb/c-lang.c
index 77753eb6c58ac63f6bfa25d541afdb1fcb2ab0f5..72a024fb7bcd2d984e8212d136eb279441153b06 100644
--- a/gdb/c-lang.c
+++ b/gdb/c-lang.c
@@ -160,9 +160,7 @@  language_defn::printchar (int c, struct type *type,
       break;
     }
 
-  gdb_putc ('\'', stream);
   generic_emit_char (c, type, stream, '\'', encoding);
-  gdb_putc ('\'', stream);
 }
 
 /* Print the character string STRING, printing at most LENGTH
diff --git a/gdb/f-lang.h b/gdb/f-lang.h
index 7ffaccacce3cddf8d3831680ae887093a0c58308..e49c58426430d20455dd806ae731c71b5ad7cbc7 100644
--- a/gdb/f-lang.h
+++ b/gdb/f-lang.h
@@ -161,10 +161,8 @@  class f_language : public language_defn
   void printchar (int ch, struct type *chtype,
 		  struct ui_file *stream) const override
   {
-    gdb_puts ("'", stream);
     const char *encoding = get_encoding (chtype);
     generic_emit_char (ch, chtype, stream, '\'', encoding);
-    gdb_puts ("'", stream);
   }
 
   /* See language.h.  */
diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c
index 2c3f2a37d38615374f7059302f52a6ab04f8fa8f..98fcc3cc589d391da804ef7fe0de252bfa19789a 100644
--- a/gdb/rust-lang.c
+++ b/gdb/rust-lang.c
@@ -1774,11 +1774,14 @@  void
 rust_language::printchar (int ch, struct type *chtype,
 			  struct ui_file *stream) const
 {
-  fputs_filtered ("'", stream);
   if (!rust_chartype_p (chtype))
-    generic_emit_char (ch, chtype, stream, '\'',
-		       target_charset (chtype->arch ()));
-  else if (ch == '\\')
+    {
+      generic_emit_char (ch, chtype, stream, '\'',
+			 target_charset (chtype->arch ()));
+      return;
+    }
+  gdb_puts ("'", stream);
+  if (ch == '\\')
     gdb_printf (stream, "\\%c", ch);
   else if (ch == '\n')
     gdb_puts ("\\n", stream);
diff --git a/gdb/valprint.c b/gdb/valprint.c
index 91f225c4e60b5621083e329d6509e7f871e7dc7b..b808a3684dc79bee277a2d4a139903cfd7ee16a5 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -2281,6 +2281,7 @@  generic_emit_char (int c, struct type *type, struct ui_file *stream,
   c_buf = (gdb_byte *) alloca (type->length ());
   pack_long (c_buf, type, c);
 
+  gdb_putc (quoter, stream);
   wchar_iterator iter (c_buf, type->length (), encoding, type->length ());
 
   /* This holds the printable form of the wchar_t data.  */
@@ -2340,6 +2341,7 @@  generic_emit_char (int c, struct type *type, struct ui_file *stream,
   obstack_1grow (&output, '\0');
 
   gdb_puts ((const char *) obstack_base (&output), stream);
+  gdb_putc (quoter, stream);
 }
 
 /* Return the repeat count of the next character/byte in ITER,