When cp_parser_string_literal fails, pragma_lex returns CPP_STRING with
error_mark_node as the value.  The pragma handlers take CPP_STRING to
mean a STRING_CST, so with checking enabled

  #pragma message "" ""a
  #pragma GCC diagnostic ignored "" ""a
  #pragma GCC optimize ("" ""a)

each report that a string with a user-defined suffix is invalid there,
then ICE in TREE_STRING_LENGTH or TREE_STRING_POINTER

The C front end does not have this problem because lex_string returns an
empty string after an error.  Do the same in the C++ pragma_lex, so the
handlers keep their assumption

gcc/cp/ChangeLog:

        PR c++/123400
        * parser.cc (pragma_lex): Return an empty string instead of
        error_mark_node for CPP_STRING.

gcc/testsuite/ChangeLog:

        PR c++/123400
        * g++.dg/cpp0x/pr123400.C: New test.

Signed-off-by: Hugo Sales <[email protected]>
---

Bootstrapped and regtested on x86_64-pc-linux-gnu.  I don't have write
access, so I'd need someone to push it

#pragma GCC diagnostic and #pragma GCC optimize still report the error
twice, once from the early pragma pass and once from the late one.  That
happened before this patch too, so I left it alone here.  I can look into
it in a separate patch if you'd like

 gcc/cp/parser.cc                      | 14 ++++++++++++--
 gcc/testsuite/g++.dg/cpp0x/pr123400.C | 16 ++++++++++++++++
 2 files changed, 28 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/pr123400.C

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index f1e2207d8bd..241b20d7e8a 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -58190,8 +58190,18 @@ pragma_lex (tree *value, location_t *loc)
   if (ret == CPP_PRAGMA_EOL)
     ret = CPP_EOF;
   else if (ret == CPP_STRING)
-    *value = cp_parser_string_literal (the_parser, /*translate=*/false,
-                                      /*wide_ok=*/false);
+    {
+      *value = cp_parser_string_literal (the_parser, /*translate=*/false,
+                                        /*wide_ok=*/false);
+      /* The pragma handlers expect a STRING_CST for CPP_STRING, so after
+        an error return an empty string, as lex_string does for C.  */
+      if (*value == error_mark_node)
+       {
+         *value = build_string (1, "");
+         TREE_TYPE (*value) = char_array_type_node;
+         *value = fix_string_type (*value);
+       }
+    }
   else
     {
       if (ret == CPP_KEYWORD)
diff --git a/gcc/testsuite/g++.dg/cpp0x/pr123400.C 
b/gcc/testsuite/g++.dg/cpp0x/pr123400.C
new file mode 100644
index 00000000000..ab562e35bad
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/pr123400.C
@@ -0,0 +1,16 @@
+// PR c++/123400
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wunused-variable" }
+
+#pragma message "" ""a // { dg-error "user-defined suffix" }
+// { dg-warning "junk at end" "" { target *-*-* } .-1 }
+#pragma GCC diagnostic ignored "" ""a // { dg-error "user-defined suffix" }
+// { dg-warning "unknown option" "" { target *-*-* } .-1 }
+#pragma GCC optimize ("" ""a) // { dg-error "user-defined suffix" }
+// { dg-warning "final" "" { target *-*-* } .-1 }
+
+// The text before the bad string is dropped, so this pragma has no effect.
+#pragma GCC diagnostic ignored "-Wunused-variable" ""a
+// { dg-error "user-defined suffix" "" { target *-*-* } .-1 }
+// { dg-warning "unknown option" "" { target *-*-* } .-2 }
+void f () { int i; } // { dg-warning "unused variable" }

base-commit: 303400bda9e33622ab24ff3c1f82866577fc7351
-- 
2.56.0

Reply via email to