From patchwork Wed Jun 29 16:11:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 55527 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7E976384D18C for ; Wed, 29 Jun 2022 16:12:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7E976384D18C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1656519140; bh=gm7bGnPdITdDguQd7/MwZemr7mDIQfdXoLoGWZzpw9U=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=IGKfDKVhTa+JSDDPf+1FkAW6tSkzoABucS7ANBb59aRs2luXXNz4s8ffUl1mut1zs s5ypzRLwtjh5fiPD5mfSKsZrxPB9DrZTjdkTq6Ak34gUBetaXZcgtPIUwBvOIFY+ce L57OiPeUgStS2wfMlkEwrXwHD7XZyJENuy2C3qiw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id DE0FC384D185 for ; Wed, 29 Jun 2022 16:11:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DE0FC384D185 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-433-xButuPt3Ojurj4kgFa58zA-1; Wed, 29 Jun 2022 12:11:49 -0400 X-MC-Unique: xButuPt3Ojurj4kgFa58zA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E429B800971 for ; Wed, 29 Jun 2022 16:11:48 +0000 (UTC) Received: from pdp-11.hsd1.ma.comcast.net (unknown [10.22.10.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id C5D4C1121314; Wed, 29 Jun 2022 16:11:48 +0000 (UTC) To: GCC Patches , Jason Merrill Subject: [PATCH] c++: warn about using keywords as identifiers [PR106111] Date: Wed, 29 Jun 2022 12:11:42 -0400 Message-Id: <20220629161142.29196-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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? 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 base-commit: b01c075e7e6d84da846c2ff9087433a30ebeb0d2 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" }