public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/omp/gcc-11] c++: Fix up attribute rollbacks in cp_parser_statement
@ 2021-07-30 16:02 Tobias Burnus
  0 siblings, 0 replies; only message in thread
From: Tobias Burnus @ 2021-07-30 16:02 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:ae7c303d04e0bd4a933d43c01e404cef037c7f4d

commit ae7c303d04e0bd4a933d43c01e404cef037c7f4d
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Jul 30 17:56:23 2021 +0200

    c++: Fix up attribute rollbacks in cp_parser_statement
    
    During the OpenMP directives using C++ attribute syntax work, I've noticed
    that cp_parser_statement when parsing various block declarations that do
    not allow attribute-specifier-seq at the start rolls back the attributes
    only if std_attrs is non-NULL (i.e. some attributes have been parsed),
    but doesn't roll back if some tokens were parsed as attribute-specifier-seq,
    but didn't yield any attributes (e.g. [[]][[]][[]][[]]), which means
    we accept those empty attributes even in places where they don't appear
    in the grammar.
    
    The following patch fixes that by instead checking if there are any
    tokens to roll back.  This makes the parsing handle the first
    function the same as the second one (where some attribute appears).
    
    The testcase contains two xfails, using namespace ... apparently
    allows attributes at the start and the attributes shall appeartain to
    using in that case.  To be fixed incrementally.
    
    2021-07-30  Jakub Jelinek  <jakub@redhat.com>
    
            * parser.c (cp_parser_statement): Rollback attributes not just
            when std_attrs is non-NULL, but whenever
            cp_parser_std_attribute_spec_seq parsed any tokens.
    
            * g++.dg/cpp0x/gen-attrs-76.C: New test.
    
    (cherry picked from commit 0ba2003cf306aa98b6ec91c9d849ab9bafcf17c2)

Diff:
---
 gcc/cp/ChangeLog.omp                      |  9 +++++++++
 gcc/cp/parser.c                           | 11 +++++++----
 gcc/testsuite/ChangeLog.omp               |  7 +++++++
 gcc/testsuite/g++.dg/cpp0x/gen-attrs-76.C | 31 +++++++++++++++++++++++++++++++
 4 files changed, 54 insertions(+), 4 deletions(-)

diff --git a/gcc/cp/ChangeLog.omp b/gcc/cp/ChangeLog.omp
index bdf818a78e6..c7c267aae69 100644
--- a/gcc/cp/ChangeLog.omp
+++ b/gcc/cp/ChangeLog.omp
@@ -1,3 +1,12 @@
+2021-07-30  Tobias Burnus  <tobias@codesourcery.com>
+
+	Backported from master:
+	2021-07-30  Jakub Jelinek  <jakub@redhat.com>
+
+	* parser.c (cp_parser_statement): Rollback attributes not just
+	when std_attrs is non-NULL, but whenever
+	cp_parser_std_attribute_spec_seq parsed any tokens.
+
 2021-07-30  Tobias Burnus  <tobias@codesourcery.com>
 
 	Backported from master:
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 8869a4884b7..0e62c5b69fd 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -11878,6 +11878,7 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr,
   cp_token *token;
   location_t statement_location, attrs_loc;
   bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
+  bool has_std_attrs;
 
  restart:
   if (if_p != NULL)
@@ -11886,7 +11887,8 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr,
   statement = NULL_TREE;
 
   saved_token_sentinel saved_tokens (parser->lexer);
-  attrs_loc = cp_lexer_peek_token (parser->lexer)->location;
+  token = cp_lexer_peek_token (parser->lexer);
+  attrs_loc = token->location;
   if (c_dialect_objc ())
     /* In obj-c++, seeing '[[' might be the either the beginning of
        c++11 attributes, or a nested objc-message-expression.  So
@@ -11900,6 +11902,7 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr,
       if (!cp_parser_parse_definitely (parser))
 	std_attrs = NULL_TREE;
     }
+  has_std_attrs = cp_lexer_peek_token (parser->lexer) != token;
 
   if (std_attrs && (flag_openmp || flag_openmp_simd))
     std_attrs = cp_parser_handle_statement_omp_attributes (parser, std_attrs);
@@ -11968,7 +11971,7 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr,
 
 	case RID_NAMESPACE:
 	  /* This must be a namespace alias definition.  */
-	  if (std_attrs != NULL_TREE)
+	  if (has_std_attrs)
 	    {
 	      /* Attributes should be parsed as part of the
 		 declaration, so let's un-parse them.  */
@@ -12073,7 +12076,7 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr,
     {
       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
 	{
-	  if (std_attrs != NULL_TREE)
+	  if (has_std_attrs)
 	    /* Attributes should be parsed as part of the declaration,
 	       so let's un-parse them.  */
 	    saved_tokens.rollback();
@@ -12085,7 +12088,7 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr,
 	  if (cp_parser_parse_definitely (parser))
 	    return;
 	  /* It didn't work, restore the post-attribute position.  */
-	  if (std_attrs)
+	  if (has_std_attrs)
 	    cp_lexer_set_token_position (parser->lexer, statement_token);
 	}
       /* All preceding labels have been parsed at this point.  */
diff --git a/gcc/testsuite/ChangeLog.omp b/gcc/testsuite/ChangeLog.omp
index 3e427ca7bc2..6437bc32db8 100644
--- a/gcc/testsuite/ChangeLog.omp
+++ b/gcc/testsuite/ChangeLog.omp
@@ -1,3 +1,10 @@
+2021-07-30  Tobias Burnus  <tobias@codesourcery.com>
+
+	Backported from master:
+	2021-07-30  Jakub Jelinek  <jakub@redhat.com>
+
+	* g++.dg/cpp0x/gen-attrs-76.C: New test.
+
 2021-07-30  Tobias Burnus  <tobias@codesourcery.com>
 
 	Backported from master:
diff --git a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-76.C b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-76.C
new file mode 100644
index 00000000000..cbda8de327e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-76.C
@@ -0,0 +1,31 @@
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wno-attributes" }
+
+namespace N {}
+namespace O { typedef int T; };
+
+void
+foo ()
+{
+  [[]] asm ("");				// { dg-error "expected" }
+  [[]] __extension__ asm ("");			// { dg-error "expected" }
+  __extension__ [[]] asm ("");			// { dg-error "expected" }
+  [[]] namespace M = ::N;			// { dg-error "expected" }
+  [[]] using namespace N;			// { dg-bogus "expected" "" { xfail *-*-* } }
+  [[]] using O::T;				// { dg-error "expected" }
+  [[]] __label__ foo;				// { dg-error "expected" }
+  [[]] static_assert (true, "");		// { dg-error "expected" }
+}
+
+void
+bar ()
+{
+  [[gnu::unused]] asm ("");			// { dg-error "expected" }
+  [[gnu::unused]] __extension__ asm ("");	// { dg-error "expected" }
+  __extension__ [[gnu::unused]] asm ("");	// { dg-error "expected" }
+  [[gnu::unused]] namespace M = ::N;		// { dg-error "expected" }
+  [[gnu::unused]] using namespace N;		// { dg-bogus "expected" "" { xfail *-*-* } }
+  [[gnu::unused]] using O::T;			// { dg-error "expected" }
+  [[gnu::unused]] __label__ foo;		// { dg-error "expected" }
+  [[gnu::unused]] static_assert (true, "");	// { dg-error "expected" }
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-07-30 16:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-30 16:02 [gcc/devel/omp/gcc-11] c++: Fix up attribute rollbacks in cp_parser_statement Tobias Burnus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).