From patchwork Tue Feb 8 08:22:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 50907 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 710303858428 for ; Tue, 8 Feb 2022 08:23:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 710303858428 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1644308580; bh=uo887olj1O1+dXaMnhmxlWc1O/KaTp67jI2op+JisSQ=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=NcbAk3KkI7TNPsEJhF9dPSWkmAAShSH4jekUFrh5izYde8LLUseH+dbEk7/cs1bJS Sm+3i4AInJmUf064FiyKN2fLwP4zm0rBlXkFol9hk59T1AO9hbwYvaCSj6nLqkgxaF WRPE8obCbqZ7Hxgin8K1qTPlFkgz/gz3YQ1YmM78= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 5CF903858D1E for ; Tue, 8 Feb 2022 08:22:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5CF903858D1E Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-580-C0ekVr3lO-CvcpRkn3dKeA-1; Tue, 08 Feb 2022 03:22:28 -0500 X-MC-Unique: C0ekVr3lO-CvcpRkn3dKeA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6F62783DD21 for ; Tue, 8 Feb 2022 08:22:27 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.125]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BBBBF282E1; Tue, 8 Feb 2022 08:22:22 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 2188MJMU3380513 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 8 Feb 2022 09:22:19 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 2188MJCw3380512; Tue, 8 Feb 2022 09:22:19 +0100 Date: Tue, 8 Feb 2022 09:22:18 +0100 To: Jason Merrill Subject: [PATCH] c++: Don't emit repeated -Wshadow warnings for templates/ctors [PR104379] Message-ID: <20220208082218.GD2646553@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi! The following patch suppresses extraneous -Wshadow warnings. On the testcase without the patch we emit 14 -Wshadow warnings, with the patch just 4. It is enough to warn once e.g. during parsing of the template or the abstract ctor, while previously we'd warn also on the clones of the ctors and on instantiation. In GCC 8 and earlier we didn't warn because check_local_shadow did /* Inline decls shadow nothing. */ if (DECL_FROM_INLINE (decl)) return; Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2022-02-08 Jakub Jelinek PR c++/104379 * name-lookup.cc (check_local_shadow): When diagnosing shadowing of a member or global declaration, add warning suppression for the decl and don't warn again on it. * g++.dg/warn/Wshadow-18.C: New test. Jakub --- gcc/cp/name-lookup.cc.jj 2022-01-18 11:58:59.000000000 +0100 +++ gcc/cp/name-lookup.cc 2022-02-07 15:00:19.781820426 +0100 @@ -3296,18 +3296,22 @@ check_local_shadow (tree decl) /* Warn if a variable shadows a non-function, or the variable is a function or a pointer-to-function. */ - if (!OVL_P (member) - || TREE_CODE (decl) == FUNCTION_DECL - || (TREE_TYPE (decl) - && (TYPE_PTRFN_P (TREE_TYPE (decl)) - || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl))))) + if ((!OVL_P (member) + || TREE_CODE (decl) == FUNCTION_DECL + || (TREE_TYPE (decl) + && (TYPE_PTRFN_P (TREE_TYPE (decl)) + || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl))))) + && !warning_suppressed_p (decl, OPT_Wshadow)) { auto_diagnostic_group d; if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow, "declaration of %qD shadows a member of %qT", decl, current_nonlambda_class_type ()) && DECL_P (member)) - inform_shadowed (member); + { + inform_shadowed (member); + suppress_warning (decl, OPT_Wshadow); + } } return; } @@ -3319,14 +3323,18 @@ check_local_shadow (tree decl) || (TREE_CODE (old) == TYPE_DECL && (!DECL_ARTIFICIAL (old) || TREE_CODE (decl) == TYPE_DECL))) - && !instantiating_current_function_p ()) + && !instantiating_current_function_p () + && !warning_suppressed_p (decl, OPT_Wshadow)) /* XXX shadow warnings in outer-more namespaces */ { auto_diagnostic_group d; if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow, "declaration of %qD shadows a global declaration", decl)) - inform_shadowed (old); + { + inform_shadowed (old); + suppress_warning (decl, OPT_Wshadow); + } return; } --- gcc/testsuite/g++.dg/warn/Wshadow-18.C.jj 2022-02-07 15:01:56.509456240 +0100 +++ gcc/testsuite/g++.dg/warn/Wshadow-18.C 2022-02-07 15:03:34.951067880 +0100 @@ -0,0 +1,22 @@ +// PR c++/104379 +// { dg-do compile } +// { dg-options "-Wshadow" } + +int x; + +template +struct S +{ + int i; + S(int i) { (void) i; } // { dg-warning "declaration of 'i' shadows a member of 'S'" } + S(float x) { (void) x; } // { dg-warning "declaration of 'x' shadows a global declaration" } + S(int *p) { int a = 1; (void) p; (void) a; + { int a = 2; (void) a; } } // { dg-warning "declaration of 'a' shadows a previous local" } +}; + +S i(1); +S j(1); +S k(1.0f); +S l(1.0f); +S m(&x); +S n(&x);