d: Fix ICE in when formating a string with '%' or '`' characters (PR98457)
authorIain Buclaw <ibuclaw@gdcproject.org>
Mon, 19 Apr 2021 16:45:32 +0000 (18:45 +0200)
committerIain Buclaw <ibuclaw@gdcproject.org>
Mon, 19 Apr 2021 17:28:09 +0000 (19:28 +0200)
The percentage character was being confused for a format specifier in
pp_format(), whilst the backtick character was confused for the
beginning of a quoted string in expand_d_format().

Both are now properly escaped to avoid the ICE.

gcc/d/ChangeLog:

PR d/98457
* d-diagnostic.cc (expand_d_format): Handle escaped backticks.
(escape_d_format): New funtion.
(verror): Call escape_d_format on prefixing strings.
(vdeprecation): Likewise.

gcc/testsuite/ChangeLog:

PR d/98457
* gdc.dg/pr98457.d: New test.

gcc/d/d-diagnostic.cc
gcc/testsuite/gdc.dg/pr98457.d [new file with mode: 0644]

index 3bf5a53..7043abe 100644 (file)
@@ -48,7 +48,7 @@ expand_d_format (const char *format)
 
   for (const char *p = format; *p;)
     {
-      while (*p != '\0' && *p != '%' && *p != '`')
+      while (*p != '\0' && *p != '\\' && *p != '%' && *p != '`')
        {
          obstack_1grow (&buf, *p);
          p++;
@@ -57,6 +57,21 @@ expand_d_format (const char *format)
       if (*p == '\0')
        break;
 
+      if (*p == '\\')
+       {
+         if (p[1] == '`')
+           {
+             /* Escaped backtick, don't expand it as a quoted string.  */
+             obstack_1grow (&buf, '`');
+             p++;;
+           }
+         else
+           obstack_1grow (&buf, *p);
+
+         p++;
+         continue;
+       }
+
       if (*p == '`')
        {
          /* Text enclosed by `...` are translated as a quoted string.  */
@@ -114,6 +129,43 @@ expand_d_format (const char *format)
   return (char *) obstack_finish (&buf);
 }
 
+/* Rewrite the format string FORMAT to deal with any characters that require
+   escaping before expand_d_format expands it.  */
+
+static char *
+escape_d_format (const char *format)
+{
+  obstack buf;
+
+  gcc_obstack_init (&buf);
+
+  for (const char *p = format; *p; p++)
+    {
+      switch (*p)
+       {
+       case '%':
+         /* Escape `%' characters so that pp_format does not confuse them
+            for actual format specifiers.  */
+         obstack_1grow (&buf, '%');
+         break;
+
+       case '`':
+         /* Escape '`' characters so that expand_d_format does not confuse them
+            for a quoted string.  */
+         obstack_1grow (&buf, '\\');
+         break;
+
+       default:
+         break;
+       }
+
+      obstack_1grow (&buf, *p);
+    }
+
+  obstack_1grow (&buf, '\0');
+  return (char *) obstack_finish (&buf);
+}
+
 /* Helper routine for all error routines.  Reports a diagnostic specified by
    KIND at the explicit location LOC.  The message FORMAT comes from the dmd
    front-end, which does not get translated by the gcc diagnostic routines.  */
@@ -177,9 +229,10 @@ verror (const Loc &loc, const char *format, va_list ap,
 
       /* Build string and emit.  */
       if (prefix2 != NULL)
-       xformat = xasprintf ("%s %s %s", prefix1, prefix2, format);
+       xformat = xasprintf ("%s %s %s", escape_d_format (prefix1),
+                            escape_d_format (prefix2), format);
       else if (prefix1 != NULL)
-       xformat = xasprintf ("%s %s", prefix1, format);
+       xformat = xasprintf ("%s %s", escape_d_format (prefix1), format);
       else
        xformat = xasprintf ("%s", format);
 
@@ -289,9 +342,10 @@ vdeprecation (const Loc &loc, const char *format, va_list ap,
 
       /* Build string and emit.  */
       if (prefix2 != NULL)
-       xformat = xasprintf ("%s %s %s", prefix1, prefix2, format);
+       xformat = xasprintf ("%s %s %s", escape_d_format (prefix1),
+                            escape_d_format (prefix2), format);
       else if (prefix1 != NULL)
-       xformat = xasprintf ("%s %s", prefix1, format);
+       xformat = xasprintf ("%s %s", escape_d_format (prefix1), format);
       else
        xformat = xasprintf ("%s", format);
 
diff --git a/gcc/testsuite/gdc.dg/pr98457.d b/gcc/testsuite/gdc.dg/pr98457.d
new file mode 100644 (file)
index 0000000..bc0d8af
--- /dev/null
@@ -0,0 +1,9 @@
+// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98457
+// { dg-do compile }
+
+void main()
+{
+    writef!"%s";    // { dg-error "template instance writef!\"%s\" template .writef. is not defined" }
+    writef!"`%s";   // { dg-error "template instance writef!\"`%s\" template .writef. is not defined" }
+    writef!"%%s`";  // { dg-error "template instance writef!\"%%s`\" template .writef. is not defined" }
+}