From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id A74803858CDA; Wed, 1 Feb 2023 02:25:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A74803858CDA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675218348; bh=So69kNxdftkdUSrAhZRn2h94qZ6WAgAUzJH90h3SQsE=; h=From:To:Subject:Date:From; b=dax+mdjLosZvTdHzg5FYjA7J3kOAqaI3HTIji9+fIcwoblArVgNGp+79N7APKbgsk uO0dmqkzG1yVL8m8t2v2l+95S0ibs4Uw0PPfEDGypOEv+7MB5sm0NtPffjS8eB7SgJ lTRug5eR0vg0UkiELLtIj6g8UQKJ+earmFVE7wo0= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-5616] c++: Add -Wno-changes-meaning X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/trunk X-Git-Oldrev: d03ae4be2c6d48255b18bb35dc25e551bd3a3b9d X-Git-Newrev: e2f939d30f5b397011d1dc06370dd8196aceebda Message-Id: <20230201022548.A74803858CDA@sourceware.org> Date: Wed, 1 Feb 2023 02:25:48 +0000 (GMT) List-Id: https://gcc.gnu.org/g:e2f939d30f5b397011d1dc06370dd8196aceebda commit r13-5616-ge2f939d30f5b397011d1dc06370dd8196aceebda Author: Jason Merrill Date: Tue Jan 31 12:56:56 2023 -0500 c++: Add -Wno-changes-meaning 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. Diff: --- 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(-) diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index b45b629069b..1eda0e0396b 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; +};