From patchwork Wed Feb 1 02:25:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 64063 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 814053858434 for ; Wed, 1 Feb 2023 02:26:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 814053858434 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675218363; bh=x4dJ96pesKfZAIO+32tPYSvqryyBauQ/rqmkfQN+RFE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=wsckBWoekOGARNtZvGhJXMymDgRcY9UHC1BvNvqBs6Xezn52k+rLHyB53grSnlCgd B3+hgApMH8SyK49TMXlCkB7Ie5nNh9D558gGvWFarj/fGuCABGwaA/ohzBUbjXBP0W /mj3uewB+OjnjBax+CVTYVyTAgKPVK4wSlZl5c3s= 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 1A95D3858D38 for ; Wed, 1 Feb 2023 02:25:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1A95D3858D38 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-633-0jHe_N2nO-SBfU3HzJ2Bqg-1; Tue, 31 Jan 2023 21:25:29 -0500 X-MC-Unique: 0jHe_N2nO-SBfU3HzJ2Bqg-1 Received: by mail-qv1-f69.google.com with SMTP id i7-20020a056214020700b004ffce246a2bso9346120qvt.3 for ; Tue, 31 Jan 2023 18:25:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=x4dJ96pesKfZAIO+32tPYSvqryyBauQ/rqmkfQN+RFE=; b=IzJRn2/eWsD6EacnbvJEafSEw5c48jxPahLR3+A4lSxnl9d0i5Pa57JsAG63JtAQS4 kLQa+RTc9/xvbsX0sffNIYMkQ/lVKptpUSh5qGNIjNJkjew4j4yyosjWaQEb/H+OdFRY uMVVvZNHzJQpvcaf/15vyi0NkuR88Jp9/lsgz07ZgREn7jKqd+01hzoJ5q71GEz34e3W u8xPLTI1kc4YAPnOTGxAo9iJqOvIhOdpSaTGqeDHyf1go22hyrgc9aMMxPRevDsu0Wri z10kToL+437pZjVRJqBcRzez4ynJEJs3c/KqI1k4XKY+cq2tW6ZKO85Q9Rfy7it+MA+k rPyQ== X-Gm-Message-State: AO0yUKVn2G/rUhDmWKElTuOOcO3BNYOuME9mJVROq5kwiApk069Vob7T zDFtEYfVBI1PMFF5qxVw2InLX5N7tAMPnTMCP2XZnL3k0helLfZjX+mJNi/8EfnA+NZ6R2S58El pvrKg4FeMsRLah9hGsSmE/ewyu4l6ZLqXkxse2Xe+/9SPHkr+WsPkEHq47Ks+ZLPCMA== X-Received: by 2002:ac8:7d88:0:b0:3ac:fba0:cde with SMTP id c8-20020ac87d88000000b003acfba00cdemr2037788qtd.22.1675218328877; Tue, 31 Jan 2023 18:25:28 -0800 (PST) X-Google-Smtp-Source: AK7set+R2VnbSJ5VJNsz2o8SChscO/CukLQGnNJlvCvp+LIke5paSg2cy+gBh0r6FTy3sxWK/JAMvg== X-Received: by 2002:ac8:7d88:0:b0:3ac:fba0:cde with SMTP id c8-20020ac87d88000000b003acfba00cdemr2037759qtd.22.1675218328403; Tue, 31 Jan 2023 18:25:28 -0800 (PST) Received: from jason.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id j20-20020ac85514000000b003b86a6449b8sm5075749qtq.85.2023.01.31.18.25.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Jan 2023 18:25:27 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: Add -Wno-changes-meaning Date: Tue, 31 Jan 2023 21:25:24 -0500 Message-Id: <20230201022524.1100674-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 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_NONE, RCVD_IN_MSPIKE_H2, 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-pc-linux-gnu, applying to trunk. This isn't exactly a regression fix, but it is in response to new build failures and safe. -- 8< -- In recent years this error has been coming up more because other compilers don't diagnose it as consistently. So let's add a flag for it, and be more lenient about cases that aren't likely to cause bugs. gcc/ChangeLog: * doc/invoke.texi: Document -Wno-changes-meaning. gcc/c-family/ChangeLog: * c.opt: Add -Wno-changes-meaning. gcc/cp/ChangeLog: * class.cc (note_name_declared_in_class): Change from permerror to -Wchanges-meaning pedwarn, forcing -pedantic-errors for most cases. gcc/testsuite/ChangeLog: * g++.dg/warn/changes-meaning2.C: New test. * g++.dg/warn/changes-meaning3.C: New test. --- gcc/doc/invoke.texi | 17 +++++++++++++++++ gcc/c-family/c.opt | 4 ++++ gcc/cp/class.cc | 18 ++++++++++++++---- gcc/testsuite/g++.dg/warn/changes-meaning2.C | 16 ++++++++++++++++ gcc/testsuite/g++.dg/warn/changes-meaning3.C | 13 +++++++++++++ 5 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/changes-meaning2.C create mode 100644 gcc/testsuite/g++.dg/warn/changes-meaning3.C base-commit: b533084d756a2696a3eb6521810e0a0b2182a8e8 diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index d12f318adfd..14d4e8a224c 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -6287,6 +6287,23 @@ union U @{ @end itemize +@item -Wno-changes-meaning @r{(C++ and Objective-C++ only)} +C++ requires that unqualified uses of a name within a class have the +same meaning in the complete scope of the class, so declaring the name +after using it is ill-formed: +@smallexample +struct A; +struct B1 @{ A a; typedef A A; @}; // warning, 'A' changes meaning +struct B2 @{ A a; struct A @{ @}; @}; // error, 'A' changes meaning +@end smallexample +By default, the B1 case is only a warning because the two declarations +have the same type, while the B2 case is an error. Both diagnostics +can be disabled with @option{-Wno-changes-meaning}. Alternately, the +error case can be reduced to a warning with +@option{-Wno-error=changes-meaning} or @option{-fpermissive}. + +Both diagnostics are also suppressed by @option{-fms-extensions}. + @item -Wchar-subscripts @opindex Wchar-subscripts @opindex Wno-char-subscripts diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index ef371ca8c26..c0fea56a8f5 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -494,6 +494,10 @@ Wcatch-value= C++ ObjC++ Var(warn_catch_value) Warning Joined RejectNegative UInteger LangEnabledBy(C++ ObjC++,Wall, 1, 0) IntegerRange(0, 3) Warn about catch handlers of non-reference type. +Wchanges-meaning +C++ ObjC++ Var(warn_changes_meaning) Warning Init(1) +Complain about a name being declared as a class member after a previous use of the same name. + Wchar-subscripts C ObjC C++ ObjC++ Var(warn_char_subscripts) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall) Warn about subscripts whose type is \"char\". diff --git a/gcc/cp/class.cc b/gcc/cp/class.cc index 351de6c5419..a2aa6674590 100644 --- a/gcc/cp/class.cc +++ b/gcc/cp/class.cc @@ -9016,7 +9016,7 @@ note_name_declared_in_class (tree name, tree decl) return; /* The C language allows members to be declared with a type of the same name, and the C++ standard says this diagnostic is not required. So - allow it in extern "C" blocks unless predantic is specified. + allow it in extern "C" blocks unless pedantic is specified. Allow it in all cases if -ms-extensions is specified. */ if ((!pedantic && current_lang_name == lang_name_c) || flag_ms_extensions) @@ -9032,9 +9032,19 @@ note_name_declared_in_class (tree name, tree decl) A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. */ - if (permerror (location_of (decl), - "declaration of %q#D changes meaning of %qD", - decl, OVL_NAME (decl))) + auto ov = make_temp_override (global_dc->pedantic_errors); + if (TREE_CODE (decl) == TYPE_DECL + && TREE_CODE (olddecl) == TYPE_DECL + && same_type_p (TREE_TYPE (decl), TREE_TYPE (olddecl))) + /* Different declaration, but same meaning; just warn. */; + else if (flag_permissive) + /* Let -fpermissive make it a warning like past versions. */; + else + /* Make it an error. */ + global_dc->pedantic_errors = 1; + if (pedwarn (location_of (decl), OPT_Wchanges_meaning, + "declaration of %q#D changes meaning of %qD", + decl, OVL_NAME (decl))) { inform (loc, "used here to mean %q#D", olddecl); inform (location_of (olddecl), "declared here" ); diff --git a/gcc/testsuite/g++.dg/warn/changes-meaning2.C b/gcc/testsuite/g++.dg/warn/changes-meaning2.C new file mode 100644 index 00000000000..7ac888c0bab --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/changes-meaning2.C @@ -0,0 +1,16 @@ +// It's an error to redeclare a name after using it in the class, but be +// lenient if it has the same meaning. + +// { dg-options "" } + +struct Lock { }; +struct Traits +{ + Lock lock; + typedef ::Lock Lock; // { dg-warning -Wchanges-meaning } +}; +struct Traits2 +{ + Lock lock; + typedef int Lock; // { dg-error -Wchanges-meaning } +}; diff --git a/gcc/testsuite/g++.dg/warn/changes-meaning3.C b/gcc/testsuite/g++.dg/warn/changes-meaning3.C new file mode 100644 index 00000000000..ffb0f3631d7 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/changes-meaning3.C @@ -0,0 +1,13 @@ +// { dg-additional-options "-Wno-changes-meaning" } + +struct Lock { }; +struct Traits +{ + Lock lock; + typedef ::Lock Lock; +}; +struct Traits2 +{ + Lock lock; + typedef int Lock; +};