PR middle-end/87052 - STRING_CST printing incomplete in Gimple dumps
authorMartin Sebor <msebor@redhat.com>
Wed, 22 Aug 2018 16:42:56 +0000 (16:42 +0000)
committerMartin Sebor <msebor@gcc.gnu.org>
Wed, 22 Aug 2018 16:42:56 +0000 (10:42 -0600)
gcc/testsuite/ChangeLog:

PR middle-end/87052
* gcc.dg/pr87052.c: New test.
* gcc.dg/tree-ssa/dump-3.c: Adjust.

gcc/ChangeLog:

PR middle-end/87052
* tree-pretty-print.c (pretty_print_string): Add argument.
(dump_generic_node): Call to pretty_print_string with string size.

From-SVN: r263781

gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pr87052.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/dump-3.c
gcc/tree-pretty-print.c

index 82e7950..606fb5f 100644 (file)
@@ -1,3 +1,9 @@
+2018-08-22  Martin Sebor  <msebor@redhat.com>
+
+       PR middle-end/87052
+       * tree-pretty-print.c (pretty_print_string): Add argument.
+       (dump_generic_node): Call to pretty_print_string with string size.
+
 2018-08-22  Segher Boessenkool  <segher@kernel.crashing.org>
 
        PR rtl-optimization/86771
index 078810e..42f10ae 100644 (file)
@@ -1,3 +1,9 @@
+2018-08-22  Martin Sebor  <msebor@redhat.com>
+
+       PR middle-end/87052
+       * gcc.dg/pr87052.c: New test.
+       * gcc.dg/tree-ssa/dump-3.c: Adjust.
+
 2018-08-22  Szabolcs Nagy  <szabolcs.nagy@arm.com>
 
        * gfortran.dg/max_fmax_aarch64.f90: Rename to...
diff --git a/gcc/testsuite/gcc.dg/pr87052.c b/gcc/testsuite/gcc.dg/pr87052.c
new file mode 100644 (file)
index 0000000..2affc48
--- /dev/null
@@ -0,0 +1,41 @@
+/* PR middle-end/87052 - STRING_CST printing incomplete in Gimple dumps
+   { dg-do compile }
+   { dg-options "-fdump-tree-gimple" } */
+
+void sink (const void*, ...);
+
+void test (void)
+{
+  const char a[3] = "\000ab";
+
+  /* Expect the following in the dump:
+     a = "\x00ab"; */
+
+  const char b[] = { 'a', 0, 'b', 'c' };
+
+  /* Expect the following:
+     b = "a\x00bc"; */
+
+  const char c[] = "";
+
+  /* Expect the following:
+     c = ""; */
+
+  const char d[0] = { };
+
+  /* Expect the following:
+     d = ""; */
+
+  const char e[0] = "";
+
+  /* Expect nothing.  */
+
+  sink (a, b, c, d, e);
+}
+
+/* { dg-final { scan-tree-dump-times "a = \"\\\\x00ab\";" 1 "gimple" } }
+   { dg-final { scan-tree-dump-times "b = \"a\\\\x00bc\";"  1 "gimple" } }
+   { dg-final { scan-tree-dump-times "c = \"\";"  1 "gimple" } }
+   { dg-final { scan-tree-dump-times "d = { *};"  1 "gimple" } }
+   { dg-final { scan-tree-dump-times "e = "  1 "gimple" } }
+   { dg-final { scan-tree-dump-times "e = {CLOBBER}"  1 "gimple" } }  */
index 8533124..6623e70 100644 (file)
@@ -263,8 +263,8 @@ int main()
   return 0;
 }
 
-/* { dg-final { scan-tree-dump "string_0 = \"\";" "optimized" } } */
-/* { dg-final { scan-tree-dump "string_4 = \"\\\\4\";" "optimized" } } */
+/* { dg-final { scan-tree-dump "string_0 = \"\\\\x00\";" "optimized" } } */
+/* { dg-final { scan-tree-dump "string_4 = \"\\\\x04\";" "optimized" } } */
 /* { dg-final { scan-tree-dump "string_28 = \"\\\\x1c\";" "optimized" } } */
 /* { dg-final { scan-tree-dump "string_51 = \"3\";" "optimized" } } */
 /* { dg-final { scan-tree-dump "string_255 = \"\\\\xff\";" "optimized" } } */
index 627d8d7..6221427 100644 (file)
@@ -37,7 +37,7 @@ along with GCC; see the file COPYING3.  If not see
 
 /* Local functions, macros and variables.  */
 static const char *op_symbol (const_tree);
-static void pretty_print_string (pretty_printer *, const char*);
+static void pretty_print_string (pretty_printer *, const char*, unsigned);
 static void newline_and_indent (pretty_printer *, int);
 static void maybe_init_pretty_print (FILE *);
 static void print_struct_decl (pretty_printer *, const_tree, int, dump_flags_t);
@@ -1800,10 +1800,13 @@ dump_generic_node (pretty_printer *pp, tree node, int spc, dump_flags_t flags,
       break;
 
     case STRING_CST:
-      pp_string (pp, "\"");
-      pretty_print_string (pp, TREE_STRING_POINTER (node));
-      pp_string (pp, "\"");
-      break;
+      {
+       pp_string (pp, "\"");
+       if (unsigned nbytes = TREE_STRING_LENGTH (node))
+         pretty_print_string (pp, TREE_STRING_POINTER (node), nbytes);
+       pp_string (pp, "\"");
+       break;
+      }
 
     case VECTOR_CST:
       {
@@ -3865,15 +3868,16 @@ print_call_name (pretty_printer *pp, tree node, dump_flags_t flags)
     }
 }
 
-/* Parses the string STR and replaces new-lines by '\n', tabs by '\t', ...  */
+/* Print the first N characters in the array STR, replacing non-printable
+   characters (including embedded nuls) with unambiguous escape sequences.  */
 
 static void
-pretty_print_string (pretty_printer *pp, const char *str)
+pretty_print_string (pretty_printer *pp, const char *str, unsigned n)
 {
   if (str == NULL)
     return;
 
-  while (*str)
+  for ( ; n; --n, ++str)
     {
       switch (str[0])
        {
@@ -3913,48 +3917,20 @@ pretty_print_string (pretty_printer *pp, const char *str)
          pp_string (pp, "\\'");
          break;
 
-         /* No need to handle \0; the loop terminates on \0.  */
-
-       case '\1':
-         pp_string (pp, "\\1");
-         break;
-
-       case '\2':
-         pp_string (pp, "\\2");
-         break;
-
-       case '\3':
-         pp_string (pp, "\\3");
-         break;
-
-       case '\4':
-         pp_string (pp, "\\4");
-         break;
-
-       case '\5':
-         pp_string (pp, "\\5");
-         break;
-
-       case '\6':
-         pp_string (pp, "\\6");
-         break;
-
-       case '\7':
-         pp_string (pp, "\\7");
-         break;
-
        default:
-         if (!ISPRINT (str[0]))
+         if (str[0] || n > 1)
            {
-             char buf[5];
-             sprintf (buf, "\\x%x", (unsigned char)str[0]);
-             pp_string (pp, buf);
+             if (!ISPRINT (str[0]))
+               {
+                 char buf[5];
+                 sprintf (buf, "\\x%02x", (unsigned char)str[0]);
+                 pp_string (pp, buf);
+               }
+             else
+               pp_character (pp, str[0]);
+             break;
            }
-         else
-           pp_character (pp, str[0]);
-         break;
        }
-      str++;
     }
 }