Hello-

This short patch addresses https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91843
by adding the needed multibyte awareness to pretty-print.c.
Together with my other patch awaiting review
(https://gcc.gnu.org/ml/gcc-patches/2019-09/msg01627.html), this fixes all
issues that I am aware of regarding printing diagnostics with multibyte
characters in UTF-8 locales. Would you please have a look and see if it's OK?
Thanks very much.

bootstrapped and tested on x86-64 Linux, all test results were identical before
and after:
34 XPASS
109 FAIL
1490 XFAIL
9470 UNSUPPORTED
332971 PASS

-Lewis
gcc/ChangeLog:

2019-10-10  Lewis Hyatt  <lhy...@gmail.com>

        PR 91853
        * pretty-print.c (pp_quoted_string): Avoid hex-escaping valid
        multibyte input. Fix off-by-one-bug printing the last byte before a
        hex-escaped output.
        (pp_character): Don't apply line wrapping in the middle of multibyte
        characters.
        (test_utf8): New test.
        (pretty_print_c_tests): Call the new test.
diff --git a/gcc/pretty-print.c b/gcc/pretty-print.c
index c57a3dbd887..742f3d23725 100644
--- a/gcc/pretty-print.c
+++ b/gcc/pretty-print.c
@@ -699,6 +699,8 @@ mingw_ansi_fputs (const char *str, FILE *fp)
 
 #endif /* __MINGW32__ */
 
+static int
+decode_utf8_char (const unsigned char *, size_t len, unsigned int *);
 static void pp_quoted_string (pretty_printer *, const char *, size_t = -1);
 
 /* Overwrite the given location/range within this text_info's rich_location.
@@ -1689,6 +1691,8 @@ void
 pp_character (pretty_printer *pp, int c)
 {
   if (pp_is_wrapping_line (pp)
+      /* If printing UTF-8, don't wrap in the middle of a sequence.  */
+      && (((unsigned int) c) & 0xC0) != 0x80
       && pp_remaining_character_count_for_line (pp) <= 0)
     {
       pp_newline (pp);
@@ -1729,8 +1733,22 @@ pp_quoted_string (pretty_printer *pp, const char *str, size_t n /* = -1 */)
       if (ISPRINT (*ps))
 	  continue;
 
+      /* Don't escape a valid UTF-8 extended char.  */
+      const unsigned char *ups = (const unsigned char *) ps;
+      if (*ups & 0x80)
+	{
+	  unsigned int extended_char;
+	  const int valid_utf8_len = decode_utf8_char (ups, n, &extended_char);
+	  if (valid_utf8_len > 0)
+	    {
+	      ps += valid_utf8_len - 1;
+	      n -= valid_utf8_len - 1;
+	      continue;
+	    }
+	}
+
       if (last < ps)
-	pp_maybe_wrap_text (pp, last, ps - 1);
+	pp_maybe_wrap_text (pp, last, ps);
 
       /* Append the hexadecimal value of the character.  Allocate a buffer
 	 that's large enough for a 32-bit char plus the hex prefix.  */
@@ -2374,6 +2392,46 @@ test_urls ()
   }
 }
 
+/* Test multibyte awareness.  */
+static void test_utf8 ()
+{
+
+  /* Check that pp_quoted_string leaves valid UTF-8 alone.  */
+  {
+    pretty_printer pp;
+    const char *s = "\xf0\x9f\x98\x82";
+    pp_quoted_string (&pp, s);
+    ASSERT_STREQ (pp_formatted_text (&pp), s);
+  }
+
+  /* Check that pp_quoted_string escapes non-UTF-8 nonprintable bytes.  */
+  {
+    pretty_printer pp;
+    pp_quoted_string (&pp, "\xf0!\x9f\x98\x82");
+    ASSERT_STREQ (pp_formatted_text (&pp),
+		  "\\xf0!\\x9f\\x98\\x82");
+  }
+
+  /* Check that pp_character will line-wrap at the beginning of a UTF-8
+     sequence, but not in the middle.  */
+  {
+      pretty_printer pp (3);
+      const char s[] = "---\xf0\x9f\x98\x82";
+      for (int i = 0; i != sizeof (s) - 1; ++i)
+	pp_character (&pp, s[i]);
+      pp_newline (&pp);
+      for (int i = 1; i != sizeof (s) - 1; ++i)
+	pp_character (&pp, s[i]);
+      pp_character (&pp, '-');
+      ASSERT_STREQ (pp_formatted_text (&pp),
+		    "---\n"
+		    "\xf0\x9f\x98\x82\n"
+		    "--\xf0\x9f\x98\x82\n"
+		    "-");
+  }
+
+}
+
 /* Run all of the selftests within this file.  */
 
 void
@@ -2383,6 +2441,7 @@ pretty_print_c_tests ()
   test_pp_format ();
   test_prefixes_and_wrapping ();
   test_urls ();
+  test_utf8 ();
 }
 
 } // namespace selftest

Reply via email to