From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id D660D3853C02 for ; Fri, 23 Jul 2021 08:03:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D660D3853C02 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-60-IWhcW0RqPTWorHMWkyex5A-1; Fri, 23 Jul 2021 04:03:30 -0400 X-MC-Unique: IWhcW0RqPTWorHMWkyex5A-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B5C87192D785 for ; Fri, 23 Jul 2021 08:03:29 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-112-143.ams2.redhat.com [10.36.112.143]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 515055D9C6; Fri, 23 Jul 2021 08:03:29 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 16N83QNF1599773 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 23 Jul 2021 10:03:26 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 16N83PMS1599772; Fri, 23 Jul 2021 10:03:25 +0200 Date: Fri, 23 Jul 2021 10:03:25 +0200 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Accept C++11 attribute-definition [PR101582] Message-ID: <20210723080325.GY2380545@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-6.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 23 Jul 2021 08:03:36 -0000 Hi! 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 instead handles it in cp_parser_toplevel_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). Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2021-07-23 Jakub Jelinek PR c++/101582 * parser.c (cp_parser_skip_std_attribute_spec_seq): Add a forward declaration. (cp_parser_toplevel_declaration): Parse attribute-declaration. * 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. --- gcc/cp/parser.c.jj 2021-07-22 17:47:26.025761491 +0200 +++ gcc/cp/parser.c 2021-07-22 19:09:28.487513184 +0200 @@ -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 @@ -14547,6 +14549,20 @@ cp_parser_toplevel_declaration (cp_parse if (cxx_dialect < cxx11) pedwarn (input_location, OPT_Wpedantic, "extra %<;%>"); } + else if (cp_lexer_nth_token_is (parser->lexer, + cp_parser_skip_std_attribute_spec_seq (parser, + 1), + CPP_SEMICOLON)) + { + location_t attrs_loc = token->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, + "attributes in attribute declaration are ignored"); + if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) + cp_lexer_consume_token (parser->lexer); + } else /* Parse the declaration itself. */ cp_parser_declaration (parser, NULL_TREE); --- gcc/testsuite/g++.dg/cpp0x/gen-attrs-45.C.jj 2020-01-12 11:54:37.072403466 +0100 +++ gcc/testsuite/g++.dg/cpp0x/gen-attrs-45.C 2021-07-22 19:14:38.250222344 +0200 @@ -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 "attributes in attribute declaration are ignored" } --- gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C.jj 2021-07-22 19:14:58.438942693 +0200 +++ gcc/testsuite/g++.dg/cpp0x/gen-attrs-75.C 2021-07-22 19:12:18.442158972 +0200 @@ -0,0 +1,8 @@ +// PR c++/101582 +// { dg-do compile } +// { dg-options "" } + +; +[[]] [[]] [[]]; // { dg-warning "attributes only available with" "" { target c++98_only } } +[[foobar]]; // { dg-warning "attributes in attribute declaration are ignored" } +// { dg-warning "attributes only available with" "" { target c++98_only } .-1 } Jakub