public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH v2 11/18] Add an emitter callback to generic_printstr and generic_emit_char
Date: Thu, 17 Feb 2022 15:05:39 -0700	[thread overview]
Message-ID: <20220217220547.3874030-12-tom@tromey.com> (raw)
In-Reply-To: <20220217220547.3874030-1-tom@tromey.com>

This adds an emitter callback to generic_printstr and
generic_emit_char.  print_wchar is renamed, exported, and modified to
be suitable for use as the default emitter.  This will be used to let
languages override the way that escape sequences are emitted.  Nothing
uses this yet, that comes later in the series.
---
 gdb/valprint.c | 134 ++++++++++++++++++++++---------------------------
 gdb/valprint.h |  65 +++++++++++++++++++++++-
 2 files changed, 122 insertions(+), 77 deletions(-)

diff --git a/gdb/valprint.c b/gdb/valprint.c
index 2d90f61e297..ecb9b3c9871 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -2163,45 +2163,16 @@ wchar_printable (gdb_wchar_t w)
 	  || w == LCST ('\v') || w == LCST ('\0'));
 }
 
-/* A ui_file that writes wide characters to an obstack.  */
-class obstack_wide_file : public ui_file
-{
-public:
-  explicit obstack_wide_file (struct obstack *output)
-    : m_output (output)
-  {
-  }
-
-  ~obstack_wide_file () = default;
-
-  void write (const char *buf, long length_buf) override
-  {
-    for (long i = 0; i < length_buf; ++i)
-      {
-	gdb_wchar_t w = gdb_btowc (buf[i]);
-	obstack_grow (m_output, &w, sizeof (gdb_wchar_t));
-      }
-  }
-
-private:
-  struct obstack *m_output;
-};
-
-/* Print a wide character W to OUTPUT.  ORIG is a pointer to the
-   original (target) bytes representing the character, ORIG_LEN is the
-   number of valid bytes.  WIDTH is the number of bytes in a base
-   characters of the type.  OUTPUT is an obstack to which wide
-   characters are emitted.  QUOTER is a (narrow) character indicating
-   the style of quotes surrounding the character to be printed.
-   NEED_ESCAPE is an in/out flag which is used to track numeric
-   escapes across calls.  */
+/* See valprint.h.  */
 
-static void
-print_wchar (gdb_wint_t w, const gdb_byte *orig,
-	     int orig_len, int width,
-	     enum bfd_endian byte_order,
-	     struct obstack *output,
-	     int quoter, bool *need_escapep)
+void
+default_emit_wchar (obstack_wide_file *stream,
+		    gdb_wint_t w,
+		    gdb::array_view<const gdb_byte> orig,
+		    int width,
+		    enum bfd_endian byte_order,
+		    int quoter,
+		    bool *need_escapep)
 {
   bool need_escape = *need_escapep;
 
@@ -2210,64 +2181,61 @@ print_wchar (gdb_wint_t w, const gdb_byte *orig,
   switch (w)
     {
       case LCST ('\a'):
-	obstack_grow_wstr (output, LCST ("\\a"));
+	fputs_filtered ("\\a", stream);
 	break;
       case LCST ('\b'):
-	obstack_grow_wstr (output, LCST ("\\b"));
+	fputs_filtered ("\\b", stream);
 	break;
       case LCST ('\f'):
-	obstack_grow_wstr (output, LCST ("\\f"));
+	fputs_filtered ("\\f", stream);
 	break;
       case LCST ('\n'):
-	obstack_grow_wstr (output, LCST ("\\n"));
+	fputs_filtered ("\\n", stream);
 	break;
       case LCST ('\r'):
-	obstack_grow_wstr (output, LCST ("\\r"));
+	fputs_filtered ("\\r", stream);
 	break;
       case LCST ('\t'):
-	obstack_grow_wstr (output, LCST ("\\t"));
+	fputs_filtered ("\\t", stream);
 	break;
       case LCST ('\v'):
-	obstack_grow_wstr (output, LCST ("\\v"));
+	fputs_filtered ("\\v", stream);
 	break;
       default:
 	{
 	  if (gdb_iswprint (w) && !(need_escape && gdb_iswxdigit (w)))
 	    {
-	      gdb_wchar_t wchar = w;
-
 	      if (w == gdb_btowc (quoter) || w == LCST ('\\'))
-		obstack_grow_wstr (output, LCST ("\\"));
-	      obstack_grow (output, &wchar, sizeof (gdb_wchar_t));
+		fputs_filtered ("\\", stream);
+	      stream->write_wide_char (w);
 	    }
 	  else
 	    {
 	      int i;
-	      obstack_wide_file file (output);
 
-	      for (i = 0; i + width <= orig_len; i += width)
+	      for (i = 0; i + width <= orig.size (); i += width)
 		{
 		  ULONGEST value;
 
 		  value = extract_unsigned_integer (&orig[i], width,
-						  byte_order);
+						    byte_order);
 		  /* If the value fits in 3 octal digits, print it that
 		     way.  Otherwise, print it as a hex escape.  */
 		  if (value <= 0777)
 		    {
-		      fprintf_filtered (&file, "\\%.3o", (int) (value & 0777));
+		      fprintf_filtered (stream, "\\%.3o", (int) (value & 0777));
 		      *need_escapep = false;
 		    }
 		  else
 		    {
-		      fprintf_filtered (&file, "\\x%lx", (long) value);
+		      fprintf_filtered (stream, "\\x%lx", (long) value);
 		      *need_escapep = true;
 		    }
 		}
 	      /* If we somehow have extra bytes, print them now.  */
-	      while (i < orig_len)
+	      while (i < orig.size ())
 		{
-		  fprintf_filtered (&file, "\\%.3o", orig[i] & 0xff);
+		  fprintf_filtered (stream, "\\%.3o", orig[i] & 0xff);
 		  *need_escapep = false;
 		  ++i;
 		}
@@ -2283,7 +2251,8 @@ print_wchar (gdb_wint_t w, const gdb_byte *orig,
 
 void
 generic_emit_char (int c, struct type *type, struct ui_file *stream,
-		   int quoter, const char *encoding)
+		   int quoter, const char *encoding,
+		   emit_char_ftype emitter)
 {
   enum bfd_endian byte_order
     = type_byte_order (type);
@@ -2297,6 +2266,7 @@ generic_emit_char (int c, struct type *type, struct ui_file *stream,
 
   /* This holds the printable form of the wchar_t data.  */
   auto_obstack wchar_buf;
+  obstack_wide_file wchar_stream (&wchar_buf);
 
   while (1)
     {
@@ -2330,16 +2300,17 @@ generic_emit_char (int c, struct type *type, struct ui_file *stream,
 	  if (!print_escape)
 	    {
 	      for (i = 0; i < num_chars; ++i)
-		print_wchar (chars[i], buf, buflen,
-			     TYPE_LENGTH (type), byte_order,
-			     &wchar_buf, quoter, &need_escape);
+		emitter (&wchar_stream, chars[i],
+			 gdb::make_array_view (buf, buflen),
+			 TYPE_LENGTH (type), byte_order,
+			 quoter, &need_escape);
 	    }
 	}
 
       /* This handles the NUM_CHARS == 0 case as well.  */
       if (print_escape)
-	print_wchar (gdb_WEOF, buf, buflen, TYPE_LENGTH (type),
-		     byte_order, &wchar_buf, quoter, &need_escape);
+	emitter (&wchar_stream, gdb_WEOF, gdb::make_array_view (buf, buflen),
+		 TYPE_LENGTH (type), byte_order, quoter, &need_escape);
     }
 
   /* The output in the host encoding.  */
@@ -2447,7 +2418,8 @@ print_converted_chars_to_obstack (struct obstack *obstack,
 				  const std::vector<converted_character> &chars,
 				  int quote_char, int width,
 				  enum bfd_endian byte_order,
-				  const struct value_print_options *options)
+				  const struct value_print_options *options,
+				  emit_char_ftype emitter)
 {
   unsigned int idx;
   const converted_character *elem;
@@ -2483,15 +2455,19 @@ print_converted_chars_to_obstack (struct obstack *obstack,
 		  obstack_grow_wstr (obstack, LCST (", "));
 		obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
 	      }
+
+	    obstack_wide_file wchar_stream (obstack);
 	    /* Output the character.  */
 	    for (j = 0; j < elem->repeat_count; ++j)
 	      {
 		if (elem->result == wchar_iterate_ok)
-		  print_wchar (elem->chars[0], elem->buf, elem->buflen, width,
-			       byte_order, obstack, quote_char, &need_escape);
+		  emitter (&wchar_stream, elem->chars[0],
+			   gdb::make_array_view (elem->buf, elem->buflen),
+			   width, byte_order, quote_char, &need_escape);
 		else
-		  print_wchar (gdb_WEOF, elem->buf, elem->buflen, width,
-			       byte_order, obstack, quote_char, &need_escape);
+		  emitter (&wchar_stream, gdb_WEOF,
+			   gdb::make_array_view (elem->buf, elem->buflen),
+			   width, byte_order, quote_char, &need_escape);
 	      }
 	  }
 	  break;
@@ -2514,12 +2490,15 @@ print_converted_chars_to_obstack (struct obstack *obstack,
 
 	    /* Output the character and repeat string.  */
 	    obstack_grow_wstr (obstack, LCST ("'"));
+	    obstack_wide_file wchar_stream (obstack);
 	    if (elem->result == wchar_iterate_ok)
-	      print_wchar (elem->chars[0], elem->buf, elem->buflen, width,
-			   byte_order, obstack, quote_char, &need_escape);
+	      emitter (&wchar_stream, elem->chars[0],
+		       gdb::make_array_view (elem->buf, elem->buflen),
+		       width, byte_order, quote_char, &need_escape);
 	    else
-	      print_wchar (gdb_WEOF, elem->buf, elem->buflen, width,
-			   byte_order, obstack, quote_char, &need_escape);
+	      emitter (&wchar_stream, gdb_WEOF,
+		       gdb::make_array_view (elem->buf, elem->buflen),
+		       width, byte_order, quote_char, &need_escape);
 	    obstack_grow_wstr (obstack, LCST ("'"));
 	    std::string s = string_printf (_(" <repeats %u times>"),
 					   elem->repeat_count);
@@ -2544,8 +2523,12 @@ print_converted_chars_to_obstack (struct obstack *obstack,
 
 	  /* Output the incomplete sequence string.  */
 	  obstack_grow_wstr (obstack, LCST ("<incomplete sequence "));
-	  print_wchar (gdb_WEOF, elem->buf, elem->buflen, width, byte_order,
-		       obstack, 0, &need_escape);
+	  {
+	    obstack_wide_file wchar_stream (obstack);
+	    emitter (&wchar_stream, gdb_WEOF,
+		     gdb::make_array_view (elem->buf, elem->buflen),
+		     width, byte_order, 0, &need_escape);
+	  }
 	  obstack_grow_wstr (obstack, LCST (">"));
 
 	  /* We do not attempt to output anything after this.  */
@@ -2604,7 +2587,8 @@ generic_printstr (struct ui_file *stream, struct type *type,
 		  const gdb_byte *string, unsigned int length, 
 		  const char *encoding, int force_ellipses,
 		  int quote_char, int c_style_terminator,
-		  const struct value_print_options *options)
+		  const struct value_print_options *options,
+		  emit_char_ftype emitter)
 {
   enum bfd_endian byte_order = type_byte_order (type);
   unsigned int i;
@@ -2680,7 +2664,7 @@ generic_printstr (struct ui_file *stream, struct type *type,
 
   /* Print the output string to the obstack.  */
   print_converted_chars_to_obstack (&wchar_buf, converted_chars, quote_char,
-				    width, byte_order, options);
+				    width, byte_order, options, emitter);
 
   if (force_ellipses || !finished)
     obstack_grow_wstr (&wchar_buf, LCST ("..."));
diff --git a/gdb/valprint.h b/gdb/valprint.h
index 0586836f9e6..0dbdef06c4c 100644
--- a/gdb/valprint.h
+++ b/gdb/valprint.h
@@ -233,14 +233,75 @@ extern void generic_value_print (struct value *val, struct ui_file *stream,
 				 const struct value_print_options *options,
 				 const struct generic_val_print_decorations *d);
 
+/* A ui_file that writes wide characters to an obstack.  */
+class obstack_wide_file : public ui_file
+{
+public:
+  explicit obstack_wide_file (struct obstack *output)
+    : m_output (output)
+  {
+  }
+
+  ~obstack_wide_file () = default;
+
+  void write (const char *buf, long length_buf) override
+  {
+    for (long i = 0; i < length_buf; ++i)
+      {
+	gdb_wchar_t w = gdb_btowc (buf[i]);
+	obstack_grow (m_output, &w, sizeof (gdb_wchar_t));
+      }
+  }
+
+  void write_wide_char (gdb_wchar_t w)
+  {
+    obstack_grow (m_output, &w, sizeof (gdb_wchar_t));
+  }
+
+private:
+  struct obstack *m_output;
+};
+
+/* A callback that can be used to print a representation of a wide
+   character to a stream.
+   STREAM is the stream to write to.
+   W is the character.  It might be gdb_WEOF, meaning an unconvertible
+   sequence.
+   ORIG is the original (target) bytes corresponding to W.
+   WIDTH is the width of a base character in the encoding.
+   BYTE_ORDER is the character type's byte order.
+   QUOTER is the quote character used -- this is a host character.
+   NEED_ESCAPEP is used to track whether emitting this character may
+   require a subsequent character to be escaped.  */
+typedef gdb::function_view<void (obstack_wide_file *stream,
+				 gdb_wint_t w,
+				 gdb::array_view<const gdb_byte> orig,
+				 int width,
+				 enum bfd_endian byte_order,
+				 int quoter,
+				 bool *need_escapep)> emit_char_ftype;
+
+/* A function suitable for use as a character emitter, that emits
+   characters in the C style.  */
+
+extern void default_emit_wchar (obstack_wide_file *stream,
+				gdb_wint_t w,
+				gdb::array_view<const gdb_byte> orig,
+				int width,
+				enum bfd_endian byte_order,
+				int quoter,
+				bool *need_escapep);
+
 extern void generic_emit_char (int c, struct type *type, struct ui_file *stream,
-			       int quoter, const char *encoding);
+			       int quoter, const char *encoding,
+			       emit_char_ftype emitter = default_emit_wchar);
 
 extern void generic_printstr (struct ui_file *stream, struct type *type, 
 			      const gdb_byte *string, unsigned int length, 
 			      const char *encoding, int force_ellipses,
 			      int quote_char, int c_style_terminator,
-			      const struct value_print_options *options);
+			      const struct value_print_options *options,
+			      emit_char_ftype emitter = default_emit_wchar);
 
 /* Run the "output" command.  ARGS and FROM_TTY are the usual
    arguments passed to all command implementations, except ARGS is
-- 
2.31.1


  parent reply	other threads:[~2022-02-17 22:05 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-17 22:05 [PATCH v2 00/18] Refactor character printing Tom Tromey
2022-02-17 22:05 ` [PATCH v2 01/18] Fix latent quote char bug in generic_printstr Tom Tromey
2022-02-17 22:05 ` [PATCH v2 02/18] Boolify need_escape in generic_emit_char Tom Tromey
2022-02-17 22:05 ` [PATCH v2 03/18] Remove c_emit_char Tom Tromey
2022-02-17 22:05 ` [PATCH v2 04/18] Remove c_printstr Tom Tromey
2022-02-17 22:05 ` [PATCH v2 05/18] Don't use wchar_printable in print_wchar Tom Tromey
2022-02-22 15:36   ` Andrew Burgess
2022-10-10 16:39     ` Tom Tromey
2022-02-17 22:05 ` [PATCH v2 06/18] Fix a latent bug " Tom Tromey
2022-02-17 22:05 ` [PATCH v2 07/18] Remove language_defn::emitchar Tom Tromey
2022-02-17 22:05 ` [PATCH v2 08/18] Add gdb_iswcntrl Tom Tromey
2022-02-17 22:05 ` [PATCH v2 09/18] Include \0 in printable wide characters Tom Tromey
2022-02-23 13:49   ` Andrew Burgess
2022-02-23 22:28     ` Tom Tromey
2022-02-23 23:59       ` Tom Tromey
2022-02-17 22:05 ` [PATCH v2 10/18] Use a ui_file in print_wchar Tom Tromey
2022-02-17 22:05 ` Tom Tromey [this message]
2022-02-17 22:05 ` [PATCH v2 12/18] Add a default encoding to generic_emit_char and generic_printstr Tom Tromey
2022-02-17 22:05 ` [PATCH v2 13/18] Change generic_emit_char to print the quotes Tom Tromey
2022-02-17 22:05 ` [PATCH v2 14/18] Use generic_emit_char in Rust Tom Tromey
2022-02-17 22:05 ` [PATCH v2 15/18] Use generic_emit_char in Ada Tom Tromey
2022-02-17 22:05 ` [PATCH v2 16/18] Use generic_emit_char in Modula-2 Tom Tromey
2022-02-23 20:17   ` Gaius Mulley
2022-03-16 12:29   ` [PATCH] Additional modula2 tests Gaius Mulley
2022-04-07 14:21     ` Tom Tromey
2022-04-09 23:16       ` Gaius Mulley
2022-04-11 19:45   ` [PATCH v1] Array access in Modula-2 Gaius Mulley
2022-02-17 22:05 ` [PATCH v2 17/18] Use generic_emit_char in Pascal Tom Tromey
2022-02-17 22:05 ` [PATCH v2 18/18] Simplify Fortran string printing Tom Tromey
2022-10-10 17:37 ` [PATCH v2 00/18] Refactor character printing 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=20220217220547.3874030-12-tom@tromey.com \
    --to=tom@tromey.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).