From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1534) id 8ED9E393BC17; Fri, 30 Jul 2021 09:53:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8ED9E393BC17 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Tobias Burnus To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/omp/gcc-11] c++: Accept C++11 attribute-definition [PR101582] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/devel/omp/gcc-11 X-Git-Oldrev: 8684b2db9b4043172926b5b7d389c1b9720ed002 X-Git-Newrev: 3f57cbd4f23b0cf45ca5b048302e08e1e2b36f3e Message-Id: <20210730095317.8ED9E393BC17@sourceware.org> Date: Fri, 30 Jul 2021 09:53:17 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 30 Jul 2021 09:53:17 -0000 https://gcc.gnu.org/g:3f57cbd4f23b0cf45ca5b048302e08e1e2b36f3e commit 3f57cbd4f23b0cf45ca5b048302e08e1e2b36f3e Author: Jakub Jelinek Date: Fri Jul 30 10:53:56 2021 +0200 c++: Accept C++11 attribute-definition [PR101582] As the following testcase shows, we don't parse properly C++11 attribute-declaration: https://eel.is/c++draft/dcl.dcl#nt:attribute-declaration cp_parser_toplevel_declaration just handles empty-declaration parsing (with diagnostics for C++98) and otherwise calls cp_parser_declaration which on it calls cp_parser_simple_declaration and rejects it with "does not declare anything" permerror. The following patch moves the handling of empty-declaration from cp_parser_toplevel_declaration to cp_parser_declaration and handles attribute-declaration in cp_parser_declaration by parsing the attributes (standard ones only, we've never supported __attribute__((...)); at namespace scope, so I'm not sure we need to introduce that), which for C++98 emits the needed diagnostics, and then warning if there are any attributes that we throw away on the floor. I'll need this later for OpenMP directives at namespace scope, e.g. [[omp::directive (requires, atomic_default_mem_order(seq_cst))]]; should be valid at namespace scope (and many other directives). 2021-07-30 Jakub Jelinek PR c++/101582 * parser.c (cp_parser_skip_std_attribute_spec_seq): Add a forward declaration. (cp_parser_declaration): Parse empty-declaration and attribute-declaration. (cp_parser_toplevel_declaration): Don't parse empty-declaration here. * g++.dg/cpp0x/gen-attrs-45.C: Expect a warning about ignored attributes instead of error. * g++.dg/cpp0x/gen-attrs-75.C: New test. * g++.dg/modules/pr101582-1.C: New test. (cherry picked from commit 77ab4e3be2d92b1ff671d58418d852195f10dd20) Diff: --- gcc/cp/ChangeLog.omp | 12 +++++++++++ gcc/cp/parser.c | 34 +++++++++++++++++++++++------- gcc/testsuite/ChangeLog.omp | 11 ++++++++++ gcc/testsuite/g++.dg/cpp0x/gen-attrs-45.C | 2 +- gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C | 35 +++++++++++++++++++++++++++++++ gcc/testsuite/g++.dg/modules/pr101582-1.C | 9 ++++++++ 6 files changed, 94 insertions(+), 9 deletions(-) diff --git a/gcc/cp/ChangeLog.omp b/gcc/cp/ChangeLog.omp index 9db3e4f26e5..bdf818a78e6 100644 --- a/gcc/cp/ChangeLog.omp +++ b/gcc/cp/ChangeLog.omp @@ -1,3 +1,15 @@ +2021-07-30 Tobias Burnus + + Backported from master: + 2021-07-30 Jakub Jelinek + + PR c++/101582 + * parser.c (cp_parser_skip_std_attribute_spec_seq): Add a forward + declaration. + (cp_parser_declaration): Parse empty-declaration and + attribute-declaration. + (cp_parser_toplevel_declaration): Don't parse empty-declaration here. + 2021-07-26 Tobias Burnus Backported from master: diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 955677e15cb..8869a4884b7 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -2507,6 +2507,8 @@ static tree cp_parser_std_attribute_spec (cp_parser *); static tree cp_parser_std_attribute_spec_seq (cp_parser *); +static size_t cp_parser_skip_std_attribute_spec_seq + (cp_parser *, size_t); static size_t cp_parser_skip_attributes_opt (cp_parser *, size_t); static bool cp_parser_extension_opt @@ -14279,6 +14281,30 @@ cp_parser_declaration (cp_parser* parser, tree prefix_attrs) cp_token *token2 = (token1->type == CPP_EOF ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2)); + if (token1->type == CPP_SEMICOLON) + { + cp_lexer_consume_token (parser->lexer); + /* A declaration consisting of a single semicolon is invalid + * before C++11. Allow it unless we're being pedantic. */ + if (cxx_dialect < cxx11) + pedwarn (input_location, OPT_Wpedantic, "extra %<;%>"); + return; + } + else if (cp_lexer_nth_token_is (parser->lexer, + cp_parser_skip_std_attribute_spec_seq (parser, + 1), + CPP_SEMICOLON)) + { + location_t attrs_loc = token1->location; + tree std_attrs = cp_parser_std_attribute_spec_seq (parser); + if (std_attrs != NULL_TREE) + warning_at (make_location (attrs_loc, attrs_loc, parser->lexer), + OPT_Wattributes, "attribute ignored"); + if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) + cp_lexer_consume_token (parser->lexer); + return; + } + /* Get the high-water mark for the DECLARATOR_OBSTACK. */ void *p = obstack_alloc (&declarator_obstack, 0); @@ -14429,14 +14455,6 @@ cp_parser_toplevel_declaration (cp_parser* parser) cp_parser_declaration. (A #pragma at block scope is handled in cp_parser_statement.) */ cp_parser_pragma (parser, pragma_external, NULL); - else if (token->type == CPP_SEMICOLON) - { - cp_lexer_consume_token (parser->lexer); - /* A declaration consisting of a single semicolon is invalid - * before C++11. Allow it unless we're being pedantic. */ - if (cxx_dialect < cxx11) - pedwarn (input_location, OPT_Wpedantic, "extra %<;%>"); - } else /* Parse the declaration itself. */ cp_parser_declaration (parser, NULL_TREE); diff --git a/gcc/testsuite/ChangeLog.omp b/gcc/testsuite/ChangeLog.omp index b560a1b2f47..3e427ca7bc2 100644 --- a/gcc/testsuite/ChangeLog.omp +++ b/gcc/testsuite/ChangeLog.omp @@ -1,3 +1,14 @@ +2021-07-30 Tobias Burnus + + Backported from master: + 2021-07-30 Jakub Jelinek + + PR c++/101582 + * g++.dg/cpp0x/gen-attrs-45.C: Expect a warning about ignored + attributes instead of error. + * g++.dg/cpp0x/gen-attrs-75.C: New test. + * g++.dg/modules/pr101582-1.C: New test. + 2021-07-27 Tobias Burnus Backported from master: diff --git a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-45.C b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-45.C index 573a1ab6cb0..2bdd549ea15 100644 --- a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-45.C +++ b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-45.C @@ -1,4 +1,4 @@ // PR c++/52906 // { dg-do compile { target c++11 } } -[[gnu::deprecated]]; // { dg-error "does not declare anything" } +[[gnu::deprecated]]; // { dg-warning "attribute ignored" } diff --git a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C new file mode 100644 index 00000000000..bac80aa02ff --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C @@ -0,0 +1,35 @@ +// PR c++/101582 +// { dg-do compile } +// { dg-options "" } + +; +[[]] [[]] [[]]; // { dg-warning "attributes only available with" "" { target c++98_only } } +[[foobar]]; // { dg-warning "attribute ignored" } +// { dg-warning "attributes only available with" "" { target c++98_only } .-1 } + +extern "C" ; +extern "C" [[]]; // { dg-warning "attributes only available with" "" { target c++98_only } } +extern "C" extern "C" ; +extern "C" extern "C" [[]][[]][[]]; // { dg-warning "attributes only available with" "" { target c++98_only } } +__extension__ ; +__extension__ [[]]; // { dg-warning "attributes only available with" "" { target c++98_only } } +__extension__ __extension__ ; +__extension__ __extension__ [[]][[]]; // { dg-warning "attributes only available with" "" { target c++98_only } } + +namespace N { + +; +[[]] [[]] [[]]; // { dg-warning "attributes only available with" "" { target c++98_only } } +[[foobar]]; // { dg-warning "attribute ignored" } +// { dg-warning "attributes only available with" "" { target c++98_only } .-1 } + +extern "C" ; +extern "C" [[]]; // { dg-warning "attributes only available with" "" { target c++98_only } } +extern "C" extern "C" ; +extern "C" extern "C" [[]][[]][[]]; // { dg-warning "attributes only available with" "" { target c++98_only } } +__extension__ ; +__extension__ [[]]; // { dg-warning "attributes only available with" "" { target c++98_only } } +__extension__ __extension__ ; +__extension__ __extension__ [[]][[]]; // { dg-warning "attributes only available with" "" { target c++98_only } } + +} diff --git a/gcc/testsuite/g++.dg/modules/pr101582-1.C b/gcc/testsuite/g++.dg/modules/pr101582-1.C new file mode 100644 index 00000000000..1d3a3fcb6fa --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/pr101582-1.C @@ -0,0 +1,9 @@ +// PR c++/101582 +// { dg-additional-options "-fmodules-ts" } +export module pr101582; +// { dg-module-cmi "pr101582" } +export ; // { dg-error "export declaration does not declare anything" "" { xfail *-*-* } } +export [[]]; // { dg-error "export declaration does not declare anything" "" { xfail *-*-* } } +export // { dg-error "export declaration does not declare anything" "" { xfail *-*-* } } +{ +}