From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by sourceware.org (Postfix) with ESMTPS id F3A823858D35 for ; Thu, 17 Aug 2023 17:07:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F3A823858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-3fe1a17f983so851615e9.3 for ; Thu, 17 Aug 2023 10:07:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692292060; x=1692896860; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:from:to:cc:subject:date:message-id :reply-to; bh=/MN2Zc9l/7nHoBgec1/6Q4tuNCHPE9fsf+wXUyaqW4Y=; b=cA2AvPgyBbQ5zq5eGl75uE/6nG6Xy0ZKiffdpaTDILJTG+lXQkNVmEUGt+lqYY/euW VxVvflBWLgPbWx9jDHBT81/jkQC1TWHhWvQaTAbCGE5bKrkFhXC50bKnaXHcMs5dQpGK 3Iz2uT8QrSoly/SNicY3G5SUp/y9eFhgn5dcrQmVbnHVUODaedhj0W7sFSyp+rgWzHJF cCcHI8vcQq1a2JOPbY9cpJkMeEtMTbQgZPcF2XEVXB4hAkFc3SJ33wAIIR2hjXqMzjCz QSJU4ai06V4aTl2OwW5tTG/jkuuoHU/IqTegMl4exI/hHiHFIEM7zTULT2qlzPy+TF9J L2lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692292060; x=1692896860; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/MN2Zc9l/7nHoBgec1/6Q4tuNCHPE9fsf+wXUyaqW4Y=; b=NLO23TX2x3lFi5naxBaewsJcWYXlADjJPRUcpeX+BYVp6OIQChhu1FmMZf2CvownRT TBTY7lv8kl4m4sUm/A/CsQs4jRgnl2XMdvh/OzPNVHUCKewBrkGQzc8zlQxoFCs6JBGM gaDqsecUchU5DwwUFgxbA1DSEW80/iu0iR941ODkYLzk6PqaChEXCV6Wu6PX8U1fPD/H cmeS9tG91WjRoW/4LBNZQufCsZkXeScuyeX/iigUdDGQMf6fag+/sFC5aFmnMg+E9vpr pZI8F4ET3yz4Mjdo/vQPfWW2eErjgEHKKnm4NlXjV2j+PX7oS2328v0HWMAJMnCRxANr w5WA== X-Gm-Message-State: AOJu0YxV+xVeWVYs4hziPdz+yiTsL7qfIHHX4c6r9fMh3u+QN7t8Jnk8 PT8jMWUTTc10jqMwnj0/XSS3jMcrAvA= X-Google-Smtp-Source: AGHT+IH4dpuF0nMXGN/P3OGV3QeZzYHtMq3n/3TutYK47Cmd7fF7/8GKdUHc964ufBDM6dUm07civw== X-Received: by 2002:a1c:7917:0:b0:3fe:1f93:8cf4 with SMTP id l23-20020a1c7917000000b003fe1f938cf4mr289232wme.8.1692292059708; Thu, 17 Aug 2023 10:07:39 -0700 (PDT) Received: from smtpclient.apple (dynamic-077-004-096-196.77.4.pool.telefonica.de. [77.4.96.196]) by smtp.gmail.com with ESMTPSA id l2-20020a5d4bc2000000b003198d274208sm7403074wrt.86.2023.08.17.10.07.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 17 Aug 2023 10:07:39 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Richard Biener Mime-Version: 1.0 (1.0) Subject: Re: [PATCH] c: Add support for [[__extension__ ...]] Date: Thu, 17 Aug 2023 19:07:28 +0200 Message-Id: References: Cc: Joseph Myers , gcc-patches@gcc.gnu.org In-Reply-To: To: Richard Sandiford X-Mailer: iPhone Mail (20G75) X-Spam-Status: No, score=-8.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_BARRACUDACENTRAL,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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 List-Id: > Am 17.08.2023 um 13:25 schrieb Richard Sandiford via Gcc-patches : >=20 > =EF=BB=BFJoseph Myers writes: >>> On Wed, 16 Aug 2023, Richard Sandiford via Gcc-patches wrote: >>>=20 >>> Would it be OK to add support for: >>>=20 >>> [[__extension__ ...]] >>>=20 >>> to suppress the pedwarn about using [[]] prior to C2X? Then we can >>=20 >> That seems like a plausible feature to add. >=20 > 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: >=20 > [[__extension__ arm::streaming]] >=20 > 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... >=20 > I wondered about trying to require the two colons to be immediately > adjacent. But: >=20 > (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.= IStR a cop Toben has ->prev_white or so > Similarly to that, it would in principle be possible to compare > two expanded locations. But... >=20 > (b) I had a vague impression that locations were allowed to drop column > information for very large inputs (maybe I'm wrong). >=20 > (c) It wouldn't cope with token pasting. >=20 > So in the end I just used a simple two-token test, like for [[ and ]]. > Bootstrapped & regression-tested on aarch64-linux-gnu. >=20 > Richard >=20 > ---- >=20 > [[]] 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.). >=20 > This patch allows the pedwarn to be suppressed by starting the > attribute-list with __extension__. >=20 > 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. >=20 > gcc/ > * doc/extend.texi: Document the C [[__extension__ ...]] construct. >=20 > 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 ::. >=20 > 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 >=20 > 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] ) >=20 > 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. */ >=20 > 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 =3D false) > { > c_token *token =3D c_parser_peek_token (parser); > tree ns, name, attribute; > @@ -5406,9 +5414,18 @@ c_parser_std_attribute (c_parser *parser, bool for_= tm) > } > name =3D 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 =3D=3D CPP_COLON)) > { > ns =3D 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 =3D c_parser_peek_token (parser); > if (token->type !=3D CPP_NAME && token->type !=3D CPP_KEYWORD) > @@ -5481,19 +5498,9 @@ c_parser_std_attribute (c_parser *parser, bool for_= tm) > } >=20 > 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 =3D false) > { > - location_t loc =3D 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 =3D NULL_TREE; > while (true) > { > @@ -5505,7 +5512,7 @@ c_parser_std_attribute_specifier (c_parser *parser, b= ool for_tm) > c_parser_consume_token (parser); > continue; > } > - tree attribute =3D c_parser_std_attribute (parser, for_tm); > + tree attribute =3D c_parser_std_attribute (parser, for_tm, loose_sc= ope_p); > if (attribute !=3D error_mark_node) > { > TREE_CHAIN (attribute) =3D 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 =3D 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 =3D disable_extension_diagnostics (); > + c_parser_consume_token (parser); > + attributes =3D 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 =3D 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 keywor= ds. It looks like this: > @findex __extension__ > @opindex pedantic > @option{-pedantic} and other options cause warnings for many GNU C extensi= ons. > -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 col= ons > +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. >=20 > @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=3Dc11 -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 +=3D 1; > + [[__extension__ fallthrough]]; > + case 19: > + case 33: > + x *=3D 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 {'unkno= wn_attribute' attribute ignored} } */ > +typedef int [[gnu::vector_size(4)]] b4; /* { dg-error {expected '\]' befo= re ':'} } */ > +/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */ > +/* { dg-error {attributes before C2X} "" { target *-*-* } .-2 } */ > +typedef int [[gnu : : vector_size(4)]] b5; /* { dg-error {expected '\]' b= efore ':'} } */ > +/* { 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=3Dc2x -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 +=3D 1; > + [[__extension__ fallthrough]]; > + case 19: > + case 33: > + x *=3D 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 {'unkno= wn_attribute' attribute ignored} } */ > +typedef int [[gnu::vector_size(4)]] b4; /* { dg-warning {attributes befor= e C2X} } */ > +typedef int [[gnu : : vector_size(4)]] b5; /* { dg-error {expected '\]' b= efore ':'} } */ > +/* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */ > +/* { dg-warning {attributes before C2X} "" { target *-*-* } .-2 } */ > --=20 > 2.25.1 >=20