We are wrongly accepting invalid code like:

  struct alignas(16 S2 { }; // missing )

The reason is that cp_parser_type_specifier uses tentative parsing to see if
we're dealing with a class-specifier, and if that doesn't work, it looks for
an elaborated-type-specifier.  When trying to parse it as a class-specifier,
we use cp_parser_class_head which parses any attributes after the class-key
via cp_parser_attributes_opt, and that includes an alignment-specifier.

So we're parsing the alignment-specifier, find the missing ')', but since we
are parsing tentatively, no error is issued.  Then we get back to
cp_parser_class_head, and that commits:

23862   /* At this point, we're going ahead with the class-specifier, even
23863      if some other problem occurs.  */
23864   cp_parser_commit_to_tentative_parse (parser);

so we lose the error.  Fixed as below.  Other approach would be to use
a tentative_firewall, but that would be a much bigger hammer.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2019-06-19  Marek Polacek  <pola...@redhat.com>

        PR c++/64235 - missing syntax error with invalid alignas.
        * parser.c (cp_parser_std_attribute_spec): Commit to tentative parse
        if there's a missing close paren.

        * g++.dg/parse/alignas1.C: New test.

diff --git gcc/cp/parser.c gcc/cp/parser.c
index 6b4aab8a12f..f88652f1f82 100644
--- gcc/cp/parser.c
+++ gcc/cp/parser.c
@@ -26371,6 +26371,12 @@ cp_parser_std_attribute_spec (cp_parser *parser)
       if (alignas_expr == error_mark_node)
        return error_mark_node;
 
+      /* Missing ')' means the code cannot possibly be valid; go ahead
+        and commit to make sure we issue a hard error.  */
+      if (cp_parser_uncommitted_to_tentative_parse_p (parser)
+         && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
+       cp_parser_commit_to_tentative_parse (parser);
+
       if (!parens.require_close (parser))
        return error_mark_node;
 
diff --git gcc/testsuite/g++.dg/parse/alignas1.C 
gcc/testsuite/g++.dg/parse/alignas1.C
new file mode 100644
index 00000000000..322c0f434f9
--- /dev/null
+++ gcc/testsuite/g++.dg/parse/alignas1.C
@@ -0,0 +1,10 @@
+// PR c++/64235
+// { dg-do compile { target c++11 } }
+
+struct S {
+  double d;
+};
+
+struct alignas(sizeof(S) S1 { }; // { dg-error "expected '\\)' before 'S1'" }
+struct alignas(16 S2 { }; // { dg-error "expected '\\)' before 'S2'" }
+struct alignas(int S3 { }; // { dg-error "expected '\\)' before 'S3'" }

Reply via email to