[PATCH 14/18] Use generic_emit_char in Rust

Tom Tromey tom@tromey.com
Wed Feb 16 13:55:14 GMT 2022


This changes the Rust code to use generic_emit_char, passing in a
function to handle Rust escape sequences correctly.  This is
PR rust/20164.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=20164
---
 gdb/rust-lang.c                    | 86 ++++++++++++++++++------------
 gdb/testsuite/gdb.rust/expr.exp    |  6 +--
 gdb/testsuite/gdb.rust/unicode.exp |  3 +-
 3 files changed, 56 insertions(+), 39 deletions(-)

diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c
index 41e973797d2..28438827c1b 100644
--- a/gdb/rust-lang.c
+++ b/gdb/rust-lang.c
@@ -218,16 +218,6 @@ rust_u8_type_p (struct type *type)
 	  && TYPE_LENGTH (type) == 1);
 }
 
-/* Return true if TYPE is a Rust character type.  */
-
-static bool
-rust_chartype_p (struct type *type)
-{
-  return (type->code () == TYPE_CODE_CHAR
-	  && TYPE_LENGTH (type) == 4
-	  && type->is_unsigned ());
-}
-
 /* If VALUE represents a trait object pointer, return the underlying
    pointer with the correct (i.e., runtime) type.  Otherwise, return
    NULL.  */
@@ -263,6 +253,57 @@ rust_get_trait_object_pointer (struct value *value)
 
 

 
+/* A callback function for generic_emit_char and generic_printstr that
+   escapes characters Rust-style.  */
+static bool
+rust_emit_char (ui_file *stream,
+		gdb_wint_t w,
+		gdb::array_view<const gdb_byte> orig,
+		int width,
+		enum bfd_endian byte_order,
+		int quoter)
+{
+  if (w == LCST ('\\'))
+    fputs_filtered ("\\\\", stream);
+  else if (w == gdb_btowc (quoter))
+    fprintf_filtered (stream, "\\%c", quoter);
+  else if (gdb_iswprint (w) && !gdb_iswcntrl (w))
+    {
+      /* Let the caller deal with all other printable characters.  */
+      return false;
+    }
+  else if (w == LCST ('\n'))
+    fputs_filtered ("\\n", stream);
+  else if (w == LCST ('\r'))
+    fputs_filtered ("\\r", stream);
+  else if (w == LCST ('\t'))
+    fputs_filtered ("\\t", stream);
+  else if (w == LCST ('\0'))
+    fputs_filtered ("\\0", stream);
+  else
+    {
+      int i;
+
+      for (i = 0; i + width <= orig.size (); i += width)
+	{
+	  ULONGEST value = extract_unsigned_integer (&orig[i], width,
+						     byte_order);
+	  if (value <= 255)
+	    fprintf_filtered (stream, "\\x%02x", (int) value);
+	  else
+	    fprintf_filtered (stream, "\\u{%06lx}", (unsigned long) value);
+	}
+
+      /* If we somehow have extra bytes, print them now.  */
+      while (i < orig.size ())
+	{
+	  fprintf_filtered (stream, "\\x%02x", orig[i] & 0xff);
+	  ++i;
+	}
+    }
+  return true;
+}
+
 /* See language.h.  */
 
 void
@@ -290,9 +331,8 @@ rust_language::printstr (struct ui_file *stream, struct type *type,
 	}
     }
 
-  /* This is not ideal as it doesn't use our character printer.  */
   generic_printstr (stream, type, string, length, encoding, force_ellipses,
-		    '"', 0, options);
+		    '"', 0, options, rust_emit_char);
 }
 
 

@@ -1595,27 +1635,7 @@ 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 == '\\')
-    fprintf_filtered (stream, "\\%c", ch);
-  else if (ch == '\n')
-    fputs_filtered ("\\n", stream);
-  else if (ch == '\r')
-    fputs_filtered ("\\r", stream);
-  else if (ch == '\t')
-    fputs_filtered ("\\t", stream);
-  else if (ch == '\0')
-    fputs_filtered ("\\0", stream);
-  else if (ch >= 32 && ch <= 127 && isprint (ch))
-    fputc_filtered (ch, stream);
-  else if (ch <= 255)
-    fprintf_filtered (stream, "\\x%02x", ch);
-  else
-    fprintf_filtered (stream, "\\u{%06x}", ch);
-  fputs_filtered ("'", stream);
+  generic_emit_char (ch, chtype, stream, '\'', nullptr, rust_emit_char);
 }
 
 /* See language.h.  */
diff --git a/gdb/testsuite/gdb.rust/expr.exp b/gdb/testsuite/gdb.rust/expr.exp
index 0c445897338..34eba4bf997 100644
--- a/gdb/testsuite/gdb.rust/expr.exp
+++ b/gdb/testsuite/gdb.rust/expr.exp
@@ -115,10 +115,8 @@ gdb_test "print \[1,2 3" "',' or ']' expected"
 gdb_test "print \[1 2" "',', ';', or ']' expected"
 
 gdb_test "print b\"hi rust\"" " = b\"hi rust\""
-# This isn't rusty syntax yet, but that's another bug -- this is just
-# testing that byte escapes work properly.
-gdb_test "print b\"\\xddhi bob\"" " = b\"\\\\335hi bob\""
-gdb_test "print b\"has\\0nul\"" " = b\"has\\\\000nul\""
+gdb_test "print b\"\\xddhi bob\"" " = b\"\\\\xddhi bob\""
+gdb_test "print b\"has\\0nul\"" " = b\"has\\\\0nul\""
 
 gdb_test "print br##\"hi\"##" " = b\"hi\""
 gdb_test "print br##\"hi" "Unexpected EOF in string"
diff --git a/gdb/testsuite/gdb.rust/unicode.exp b/gdb/testsuite/gdb.rust/unicode.exp
index 9de0a0e724f..8378195bf5d 100644
--- a/gdb/testsuite/gdb.rust/unicode.exp
+++ b/gdb/testsuite/gdb.rust/unicode.exp
@@ -43,8 +43,7 @@ if {![runto ${srcfile}:$line]} {
 
 gdb_test "print 𝕯" " = 98" "print D"
 gdb_test "print \"𝕯\"" " = \"𝕯\"" "print D in string"
-# This output is maybe not ideal, but it also isn't incorrect.
-gdb_test "print '𝕯'" " = 120175 '\\\\u\\\{01d56f\\\}'" \
+gdb_test "print '𝕯'" " = 120175 '𝕯'" \
     "print D as char"
 gdb_test "print cç" " = 97" "print cc"
 
-- 
2.31.1



More information about the Gdb-patches mailing list