public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Marek Polacek <polacek@redhat.com>,
	GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] c++: warn about using keywords as identifiers [PR106111]
Date: Fri, 1 Jul 2022 11:05:42 -0400	[thread overview]
Message-ID: <88d81423-f0c0-a0da-f08f-94dad002d78b@redhat.com> (raw)
In-Reply-To: <20220629161142.29196-1-polacek@redhat.com>

On 6/29/22 12:11, Marek Polacek wrote:
> In C++03, -Wc++11-compat should warn about
> 
>    int constexpr;
> 
> since 'constexpr' is a keyword in C++11.  Jonathan reports that
> we don't emit a similar warning for 'alignas' or 'alignof', and,
> as I found out, 'thread_local'.
> 
> Similarly, we don't warn for most C++20 keywords.  That happens
> because RID_LAST_CXX20 hasn't been updated in a while.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.

> 	PR c++/106111
> 
> gcc/c-family/ChangeLog:
> 
> 	* c-common.h (enum rid): Update RID_LAST_CXX20.
> 
> gcc/cp/ChangeLog:
> 
> 	* parser.cc (cp_lexer_get_preprocessor_token): Also warn about
> 	RID_ALIGNOF, RID_ALIGNAS, RID_THREAD.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp0x/keywords1.C: New test.
> 	* g++.dg/cpp2a/keywords1.C: New test.
> ---
>   gcc/c-family/c-common.h                |  2 +-
>   gcc/cp/parser.cc                       | 10 +++++++---
>   gcc/testsuite/g++.dg/cpp0x/keywords1.C | 15 +++++++++++++++
>   gcc/testsuite/g++.dg/cpp2a/keywords1.C | 12 ++++++++++++
>   4 files changed, 35 insertions(+), 4 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/keywords1.C
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/keywords1.C
> 
> diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
> index 47442c95a53..a1e6a55158d 100644
> --- a/gcc/c-family/c-common.h
> +++ b/gcc/c-family/c-common.h
> @@ -271,7 +271,7 @@ enum rid
>     RID_FIRST_CXX11 = RID_CONSTEXPR,
>     RID_LAST_CXX11 = RID_STATIC_ASSERT,
>     RID_FIRST_CXX20 = RID_CONSTINIT,
> -  RID_LAST_CXX20 = RID_CONSTINIT,
> +  RID_LAST_CXX20 = RID_CO_RETURN,
>     RID_FIRST_AT = RID_AT_ENCODE,
>     RID_LAST_AT = RID_AT_IMPLEMENTATION,
>     RID_FIRST_PQ = RID_IN,
> diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
> index da2f370cdca..cc6525e0509 100644
> --- a/gcc/cp/parser.cc
> +++ b/gcc/cp/parser.cc
> @@ -890,10 +890,14 @@ cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
>         else
>   	{
>             if (warn_cxx11_compat
> -              && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
> -              && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
> +	      && ((C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
> +		   && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
> +		  /* These are outside the CXX11 range.  */
> +		  || C_RID_CODE (token->u.value) == RID_ALIGNOF
> +		  || C_RID_CODE (token->u.value) == RID_ALIGNAS
> +		  || C_RID_CODE (token->u.value)== RID_THREAD))
>               {
> -              /* Warn about the C++0x keyword (but still treat it as
> +	      /* Warn about the C++11 keyword (but still treat it as
>                    an identifier).  */
>   	      warning_at (token->location, OPT_Wc__11_compat,
>   			  "identifier %qE is a keyword in C++11",
> diff --git a/gcc/testsuite/g++.dg/cpp0x/keywords1.C b/gcc/testsuite/g++.dg/cpp0x/keywords1.C
> new file mode 100644
> index 00000000000..2b2ab6404ea
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/keywords1.C
> @@ -0,0 +1,15 @@
> +// PR c++/106111
> +// { dg-do compile { target c++98_only } }
> +// { dg-options "-Wc++11-compat" }
> +
> +int alignof; // { dg-warning "is a keyword in C\\\+\\\+11" }
> +int alignas; // { dg-warning "is a keyword in C\\\+\\\+11" }
> +int constexpr; // { dg-warning "is a keyword in C\\\+\\\+11" }
> +int decltype; // { dg-warning "is a keyword in C\\\+\\\+11" }
> +int noexcept; // { dg-warning "is a keyword in C\\\+\\\+11" }
> +int nullptr; // { dg-warning "is a keyword in C\\\+\\\+11" }
> +int static_assert; // { dg-warning "is a keyword in C\\\+\\\+11" }
> +int thread_local; // { dg-warning "is a keyword in C\\\+\\\+11" }
> +int _Alignas;
> +int _Alignof;
> +int _Thread_local;
> diff --git a/gcc/testsuite/g++.dg/cpp2a/keywords1.C b/gcc/testsuite/g++.dg/cpp2a/keywords1.C
> new file mode 100644
> index 00000000000..7f4dba2d3b7
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/keywords1.C
> @@ -0,0 +1,12 @@
> +// PR c++/106111
> +// { dg-do compile { target c++17_down } }
> +// { dg-options "-Wc++20-compat -Wc++11-compat -Wc++14-compat -Wc++17-compat" }
> +
> +int constinit; // { dg-warning "is a keyword in C\\\+\\\+20" }
> +int consteval; // { dg-warning "is a keyword in C\\\+\\\+20" }
> +int requires; // { dg-warning "is a keyword in C\\\+\\\+20" }
> +int concept; // { dg-warning "is a keyword in C\\\+\\\+20" }
> +int co_await; // { dg-warning "is a keyword in C\\\+\\\+20" }
> +int co_yield; // { dg-warning "is a keyword in C\\\+\\\+20" }
> +int co_return; // { dg-warning "is a keyword in C\\\+\\\+20" }
> +int char8_t; // { dg-warning "is a keyword in C\\\+\\\+20" }
> 
> base-commit: b01c075e7e6d84da846c2ff9087433a30ebeb0d2


      reply	other threads:[~2022-07-01 15:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-29 16:11 Marek Polacek
2022-07-01 15:05 ` Jason Merrill [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=88d81423-f0c0-a0da-f08f-94dad002d78b@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=polacek@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).