From patchwork Thu Aug 17 11:24:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 1822286 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=UBwxPuyo; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RRN2x0SDXz1yNl for ; Thu, 17 Aug 2023 21:25:13 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0E35D38555B9 for ; Thu, 17 Aug 2023 11:25:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0E35D38555B9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692271511; bh=ukJAST5CIBuLZZCjuVtlSEP2jfB7J9RZpvpxcGJ9e4w=; h=To:Cc:Subject:References:Date:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=UBwxPuyoQUNpOwdWIH6zk1RsOwnh/FFCg9mj5K9CsseTiQ3Het9Rr+JrYPrImc4XO hsy0XjVbiTO0X1gJC0Yh91eyOCl4ATekXQXZIzbcsfuu5o62vYNUjgM9Y1zMlgROBH F5qWiz9SA4keY1223E/kKKqsaHpm0bwfYY9nB+5E= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 5C5F63858D1E for ; Thu, 17 Aug 2023 11:24:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5C5F63858D1E Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 10804D75; Thu, 17 Aug 2023 04:25:31 -0700 (PDT) Received: from localhost (e121540-lin.manchester.arm.com [10.32.110.72]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A3CEA3F762; Thu, 17 Aug 2023 04:24:49 -0700 (PDT) To: Joseph Myers Mail-Followup-To: Joseph Myers , gcc-patches@gcc.gnu.org, richard.sandiford@arm.com Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c: Add support for [[__extension__ ...]] References: <4ea49f3-954f-e4aa-1721-8258f1bb5f75@codesourcery.com> <50a9b1a1-9efb-a170-34a1-c1e98f69afa@codesourcery.com> Date: Thu, 17 Aug 2023 12:24:48 +0100 In-Reply-To: <50a9b1a1-9efb-a170-34a1-c1e98f69afa@codesourcery.com> (Joseph Myers's message of "Wed, 16 Aug 2023 13:22:18 +0000") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 X-Spam-Status: No, score=-25.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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-Patchwork-Original-From: Richard Sandiford via Gcc-patches From: Richard Sandiford Reply-To: Richard Sandiford Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Joseph Myers writes: > On Wed, 16 Aug 2023, Richard Sandiford via Gcc-patches wrote: > >> Would it be OK to add support for: >> >> [[__extension__ ...]] >> >> to suppress the pedwarn about using [[]] prior to C2X? Then we can > > That seems like a plausible feature to add. Thanks. Of course, once I actually tried it, I hit a snag: :: isn't a single lexing token prior to C2X, and so something like: [[__extension__ arm::streaming]] would not be interpreted as a scoped attribute in C11. The patch gets around that by allowing two colons in place of :: when __extension__ is used. I realise that's pushing the bounds of acceptability though... I wondered about trying to require the two colons to be immediately adjacent. But: (a) There didn't appear to be an existing API to check that, which seemed like a red flag. The closest I could find was get_source_text_between. Similarly to that, it would in principle be possible to compare two expanded locations. But... (b) I had a vague impression that locations were allowed to drop column information for very large inputs (maybe I'm wrong). (c) It wouldn't cope with token pasting. So in the end I just used a simple two-token test, like for [[ and ]]. Bootstrapped & regression-tested on aarch64-linux-gnu. Richard ---- [[]] attributes are a recent addition to C, but as a GNU extension, GCC allows them to be used in C11 and earlier. Normally this use would trigger a pedwarn (for -pedantic, -Wc11-c2x-compat, etc.). This patch allows the pedwarn to be suppressed by starting the attribute-list with __extension__. Also, :: is not a single lexing token prior to C2X, so it wasn't possible to use scoped attributes in C11, even as a GNU extension. The patch allows two colons to be used in place of :: when __extension__ is used. No attempt is made to check whether the two colons are immediately adjacent. gcc/ * doc/extend.texi: Document the C [[__extension__ ...]] construct. gcc/c/ * c-parser.cc (c_parser_std_attribute): Conditionally allow two colons to be used in place of ::. (c_parser_std_attribute_list): New function, split out from... (c_parser_std_attribute_specifier): ...here. Allow the attribute-list to start with __extension__. When it does, also allow two colons to be used in place of ::. gcc/testsuite/ * gcc.dg/c2x-attr-syntax-6.c: New test. * gcc.dg/c2x-attr-syntax-7.c: Likewise. --- gcc/c/c-parser.cc | 68 ++++++++++++++++++------ gcc/doc/extend.texi | 27 ++++++++-- gcc/testsuite/gcc.dg/c2x-attr-syntax-6.c | 50 +++++++++++++++++ gcc/testsuite/gcc.dg/c2x-attr-syntax-7.c | 48 +++++++++++++++++ 4 files changed, 173 insertions(+), 20 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/c2x-attr-syntax-6.c create mode 100644 gcc/testsuite/gcc.dg/c2x-attr-syntax-7.c diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index 33fe7b115ff..82e56b28446 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -5390,10 +5390,18 @@ c_parser_balanced_token_sequence (c_parser *parser) ( balanced-token-sequence[opt] ) Keywords are accepted as identifiers for this purpose. -*/ + + As an extension, we permit an attribute-specifier to be: + + [ [ __extension__ attribute-list ] ] + + Two colons are then accepted as a synonym for ::. No attempt is made + to check whether the colons are immediately adjacent. LOOSE_SCOPE_P + indicates whether this relaxation is in effect. */ static tree -c_parser_std_attribute (c_parser *parser, bool for_tm) +c_parser_std_attribute (c_parser *parser, bool for_tm, + bool loose_scope_p = false) { c_token *token = c_parser_peek_token (parser); tree ns, name, attribute; @@ -5406,9 +5414,18 @@ c_parser_std_attribute (c_parser *parser, bool for_tm) } name = canonicalize_attr_name (token->value); c_parser_consume_token (parser); - if (c_parser_next_token_is (parser, CPP_SCOPE)) + if (c_parser_next_token_is (parser, CPP_SCOPE) + || (loose_scope_p + && c_parser_next_token_is (parser, CPP_COLON) + && c_parser_peek_token (parser)->type == CPP_COLON)) { ns = name; + if (c_parser_next_token_is (parser, CPP_COLON)) + { + c_parser_consume_token (parser); + if (!c_parser_next_token_is (parser, CPP_COLON)) + gcc_unreachable (); + } c_parser_consume_token (parser); token = c_parser_peek_token (parser); if (token->type != CPP_NAME && token->type != CPP_KEYWORD) @@ -5481,19 +5498,9 @@ c_parser_std_attribute (c_parser *parser, bool for_tm) } static tree -c_parser_std_attribute_specifier (c_parser *parser, bool for_tm) +c_parser_std_attribute_list (c_parser *parser, bool for_tm, + bool loose_scope_p = false) { - location_t loc = c_parser_peek_token (parser)->location; - if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>")) - return NULL_TREE; - if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>")) - { - c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>"); - return NULL_TREE; - } - if (!for_tm) - pedwarn_c11 (loc, OPT_Wpedantic, - "ISO C does not support %<[[]]%> attributes before C2X"); tree attributes = NULL_TREE; while (true) { @@ -5505,7 +5512,7 @@ c_parser_std_attribute_specifier (c_parser *parser, bool for_tm) c_parser_consume_token (parser); continue; } - tree attribute = c_parser_std_attribute (parser, for_tm); + tree attribute = c_parser_std_attribute (parser, for_tm, loose_scope_p); if (attribute != error_mark_node) { TREE_CHAIN (attribute) = attributes; @@ -5514,6 +5521,35 @@ c_parser_std_attribute_specifier (c_parser *parser, bool for_tm) if (c_parser_next_token_is_not (parser, CPP_COMMA)) break; } + return attributes; +} + +static tree +c_parser_std_attribute_specifier (c_parser *parser, bool for_tm) +{ + location_t loc = c_parser_peek_token (parser)->location; + if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>")) + return NULL_TREE; + if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>")) + { + c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>"); + return NULL_TREE; + } + tree attributes; + if (c_parser_next_token_is_keyword (parser, RID_EXTENSION)) + { + auto ext = disable_extension_diagnostics (); + c_parser_consume_token (parser); + attributes = c_parser_std_attribute_list (parser, for_tm, true); + restore_extension_diagnostics (ext); + } + else + { + if (!for_tm) + pedwarn_c11 (loc, OPT_Wpedantic, + "ISO C does not support %<[[]]%> attributes before C2X"); + attributes = c_parser_std_attribute_list (parser, for_tm); + } c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>"); c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>"); return nreverse (attributes); diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 73a997276cb..d764e44cbce 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -11914,10 +11914,29 @@ macros to replace them with the customary keywords. It looks like this: @findex __extension__ @opindex pedantic @option{-pedantic} and other options cause warnings for many GNU C extensions. -You can -prevent such warnings within one expression by writing -@code{__extension__} before the expression. @code{__extension__} has no -effect aside from this. +You can suppress such warnings using the keyword @code{__extension__}. +Specifically: + +@itemize @bullet +@item +Writing @code{__extension__} before an expression prevents warnings +about extensions within that expression. + +@item +In C, writing: + +@smallexample +[[__extension__ @dots{}]] +@end smallexample + +suppresses warnings about using @samp{[[]]} attributes in C versions +that predate C2X@. Since the scope token @samp{::} is not a single +lexing token in earlier versions of C, this construct also allows two colons +to be used in place of @code{::}. GCC does not check whether the two +colons are immediately adjacent. +@end itemize + +@code{__extension__} has no effect aside from this. @node Incomplete Enums @section Incomplete @code{enum} Types diff --git a/gcc/testsuite/gcc.dg/c2x-attr-syntax-6.c b/gcc/testsuite/gcc.dg/c2x-attr-syntax-6.c new file mode 100644 index 00000000000..01eff1f32df --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-attr-syntax-6.c @@ -0,0 +1,50 @@ +/* Test C2x attribute syntax: use of __extension__ in C11 mode. */ +/* { dg-do compile } */ +/* { dg-options "-std=c11 -pedantic-errors" } */ + +#define FOO :: +#define BAR : + +typedef int [[__extension__ gnu :: vector_size (4)]] g1; +typedef int [[__extension__ gnu : : vector_size (4)]] g2; +typedef int [[__extension__ gnu FOO vector_size (4)]] g3; +typedef int [[__extension__ gnu BAR BAR vector_size (4)]] g4; +typedef int [[__extension__ gnu :: vector_size (sizeof (void (*)(...)))]] g5; +typedef int [[__extension__]] g6; +typedef int [[__extension__,]] g7; +typedef int [[__extension__, ,,,, ,, ,]] g8; +[[__extension__ deprecated]] int g9 (); +[[__extension__ nodiscard]] int g10 (); +[[__extension__ noreturn]] void g11 (); + +int +cases (int x) +{ + switch (x) + { + case 1: + case 2: + case 4: + x += 1; + [[__extension__ fallthrough]]; + case 19: + case 33: + x *= 2; + [[fallthrough]]; /* { dg-error {attributes before C2X} } */ + case 99: + return x; + + default: + return 0; + } +} + +typedef int [[__extension__ vector_size (4)]] b1; /* { dg-error {'vector_size' attribute ignored} } */ +typedef int [[__extension__ __extension__]] b2; /* { dg-error {'extension' attribute ignored} } */ +typedef int [[__extension__ unknown_attribute]] b3; /* { dg-error {'unknown_attribute' attribute ignored} } */ +typedef int [[gnu::vector_size(4)]] b4; /* { dg-error {expected '\]' before ':'} } */ +/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */ +/* { dg-error {attributes before C2X} "" { target *-*-* } .-2 } */ +typedef int [[gnu : : vector_size(4)]] b5; /* { dg-error {expected '\]' before ':'} } */ +/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */ +/* { dg-error {attributes before C2X} "" { target *-*-* } .-2 } */ diff --git a/gcc/testsuite/gcc.dg/c2x-attr-syntax-7.c b/gcc/testsuite/gcc.dg/c2x-attr-syntax-7.c new file mode 100644 index 00000000000..cfa9b00cfc9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-attr-syntax-7.c @@ -0,0 +1,48 @@ +/* Test C2x attribute syntax: use of __extension__ in C11 mode. */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x -pedantic-errors -Wc11-c2x-compat" } */ + +#define FOO :: +#define BAR : + +typedef int [[__extension__ gnu :: vector_size (4)]] g1; +typedef int [[__extension__ gnu : : vector_size (4)]] g2; +typedef int [[__extension__ gnu FOO vector_size (4)]] g3; +typedef int [[__extension__ gnu BAR BAR vector_size (4)]] g4; +typedef int [[__extension__ gnu :: vector_size (sizeof (void (*)(...)))]] g5; +typedef int [[__extension__]] g6; +typedef int [[__extension__,]] g7; +typedef int [[__extension__, ,,,, ,, ,]] g8; +[[__extension__ deprecated]] int g9 (); +[[__extension__ nodiscard]] int g10 (); +[[__extension__ noreturn]] void g11 (); + +int +cases (int x) +{ + switch (x) + { + case 1: + case 2: + case 4: + x += 1; + [[__extension__ fallthrough]]; + case 19: + case 33: + x *= 2; + [[fallthrough]]; /* { dg-warning {attributes before C2X} } */ + case 99: + return x; + + default: + return 0; + } +} + +typedef int [[__extension__ vector_size (4)]] b1; /* { dg-error {'vector_size' attribute ignored} } */ +typedef int [[__extension__ __extension__]] b2; /* { dg-error {'extension' attribute ignored} } */ +typedef int [[__extension__ unknown_attribute]] b3; /* { dg-error {'unknown_attribute' attribute ignored} } */ +typedef int [[gnu::vector_size(4)]] b4; /* { dg-warning {attributes before C2X} } */ +typedef int [[gnu : : vector_size(4)]] b5; /* { dg-error {expected '\]' before ':'} } */ +/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */ +/* { dg-warning {attributes before C2X} "" { target *-*-* } .-2 } */