From patchwork Fri Aug 22 18:24:10 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?TWFudWVsIEzDs3Blei1JYsOhw7Fleg==?= X-Patchwork-Id: 382312 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id D114A1400E9 for ; Sat, 23 Aug 2014 04:24:44 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; q=dns; s=default; b=ekFqpgMlWPsEb// ip8d327K/WeK5uMu2+99T0bMF2IW6DjSYv10tr1vfaGATtYt5SfYSZ497AsOcQFZ Ows0xno3RPyIzWWLWTuQAXnucF/fG7lMHfyXpHQJLmhjFC3aUe5FNabRZS7VwTsc sSYA3l9cDXCglvFc8cODdDszainA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; s=default; bh=q85q0J95S1mEJjJwtM9QU pAeaCM=; b=fIRPm/4MHsf1uw4baFKzjsch+vQVNVikADZ9fnA/Prr9KbeJCB+DK OL+VZaqPzItpA+gOR2m8NdJeYMnmsTjRiaClsx/8TsgnYH8mP79xNiiGOx/6+KW/ 84UFmV9LnngkaziGd3Xchw8vwYTyAwGGFjp8FM1mYbCWeMDKyxIWFE= Received: (qmail 29932 invoked by alias); 22 Aug 2014 18:24:37 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 29914 invoked by uid 89); 22 Aug 2014 18:24:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.2 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-wi0-f176.google.com Received: from mail-wi0-f176.google.com (HELO mail-wi0-f176.google.com) (209.85.212.176) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Fri, 22 Aug 2014 18:24:35 +0000 Received: by mail-wi0-f176.google.com with SMTP id bs8so79019wib.3 for ; Fri, 22 Aug 2014 11:24:32 -0700 (PDT) X-Received: by 10.180.86.65 with SMTP id n1mr122370wiz.41.1408731872310; Fri, 22 Aug 2014 11:24:32 -0700 (PDT) MIME-Version: 1.0 Received: by 10.217.80.73 with HTTP; Fri, 22 Aug 2014 11:24:10 -0700 (PDT) In-Reply-To: <53F6713B.9050400@oracle.com> References: <53F66607.5070305@redhat.com> <53F6713B.9050400@oracle.com> From: =?ISO-8859-1?Q?Manuel_L=F3pez=2DIb=E1=F1ez?= Date: Fri, 22 Aug 2014 20:24:10 +0200 Message-ID: Subject: Re: [PATCH c++/57709] Wshadow is too strict / has false positives To: Paolo Carlini Cc: Jason Merrill , Gcc Patch List X-IsSubscribed: yes On 22 August 2014 00:22, Paolo Carlini wrote: > Hi, > > > On 08/22/2014 12:01 AM, Manuel López-Ibáñez wrote: >> >> On 21 August 2014 23:35, Jason Merrill wrote: >>> >>> On 08/21/2014 04:22 PM, Manuel López-Ibáńez wrote: >>>> >>>> + && TREE_CODE (x) != FUNCTION_DECL >>>> + && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (x)))) >>> >>> >>> How about pointer to member function? >> >> Maybe like this? BTW, I did not actually want to include the >> gcc_assert(), it was just a sanity check, thus I deleted it from this >> patch. > > Note by the way, that this patch would introduce the *first* use of > FUNCTION_POINTER_TYPE_P in the C++ front-end. Are we sure we want to use > that vs TYPE_PTRFN_P?!? At the moment I'm a bit tired by I seem to remember > subtleties in this area... I took all the comments above into consideration and produced this new version. Since the comments in cp-tree.h did actually confuse me more than help, I decided to make them more consistent. This could be committed together, separated or not at all as you wish. Bootstrapped and regression tested. gcc/cp/ChangeLog: 2014-08-22 Manuel López-Ibáñez PR c++/57709 * name-lookup.c (pushdecl_maybe_friend_1): Do not warn if a declaration shadows a function declaration, unless the former declares a function, pointer to function or pointer to member function, because this is a common and valid case in real-world code. * cp-tree.h (TYPE_PTRFN_P,TYPE_REFFN_P,TYPE_PTRMEMFUNC_P): Improve description. gcc/testsuite/ChangeLog: 2014-08-22 Manuel López-Ibáñez PR c++/57709 * g++.dg/Wshadow.C: New test. Index: gcc/testsuite/g++.dg/Wshadow.C =================================================================== --- gcc/testsuite/g++.dg/Wshadow.C (revision 0) +++ gcc/testsuite/g++.dg/Wshadow.C (revision 0) @@ -0,0 +1,15 @@ +// { dg-do compile } +// { dg-options "-Wshadow" } +// PR c++/57709 +class C { + int both_var; // { dg-message "declaration" } + void var_and_method(void) {} // { dg-message "declaration" } + void m() { + int + both_var, // { dg-warning "shadows" } + var_and_method; + } + void m2() { + void (C::*var_and_method)(void); // { dg-warning "shadows" } + } +}; Index: gcc/cp/cp-tree.h =================================================================== --- gcc/cp/cp-tree.h (revision 214229) +++ gcc/cp/cp-tree.h (working copy) @@ -3556,22 +3556,21 @@ more_aggr_init_expr_args_p (const aggr_i #define TYPE_PTROBV_P(NODE) \ (TYPE_PTR_P (NODE) \ && !(TREE_CODE (TREE_TYPE (NODE)) == FUNCTION_TYPE \ || TREE_CODE (TREE_TYPE (NODE)) == METHOD_TYPE)) -/* Returns true if NODE is a pointer to function. */ +/* Returns true if NODE is a pointer to function type. */ #define TYPE_PTRFN_P(NODE) \ (TYPE_PTR_P (NODE) \ && TREE_CODE (TREE_TYPE (NODE)) == FUNCTION_TYPE) -/* Returns true if NODE is a reference to function. */ +/* Returns true if NODE is a reference to function type. */ #define TYPE_REFFN_P(NODE) \ (TREE_CODE (NODE) == REFERENCE_TYPE \ && TREE_CODE (TREE_TYPE (NODE)) == FUNCTION_TYPE) -/* Nonzero for _TYPE node means that this type is a pointer to member - function type. */ +/* Returns true if NODE is a pointer to member function type. */ #define TYPE_PTRMEMFUNC_P(NODE) \ (TREE_CODE (NODE) == RECORD_TYPE \ && TYPE_PTRMEMFUNC_FLAG (NODE)) #define TYPE_PTRMEMFUNC_FLAG(NODE) \ Index: gcc/cp/name-lookup.c =================================================================== --- gcc/cp/name-lookup.c (revision 214229) +++ gcc/cp/name-lookup.c (working copy) @@ -1237,13 +1237,28 @@ pushdecl_maybe_friend_1 (tree x, bool is else member = NULL_TREE; if (member && !TREE_STATIC (member)) { - /* Location of previous decl is not useful in this case. */ - warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'", - x); + if (BASELINK_P (member)) + member = BASELINK_FUNCTIONS (member); + member = OVL_CURRENT (member); + + /* Do not warn if a variable shadows a function, unless + the variable is a function or a pointer-to-function. */ + if (TREE_CODE (member) != FUNCTION_DECL + || TREE_CODE (x) == FUNCTION_DECL + || TYPE_PTRFN_P (TREE_TYPE (x)) + || TYPE_PTRMEMFUNC_P (TREE_TYPE (x))) + { + if (warning_at (input_location, OPT_Wshadow, + "declaration of %qD shadows a member of %qT", + x, current_nonlambda_class_type ()) + && DECL_P(member)) + inform (DECL_SOURCE_LOCATION (member), + "shadowed declaration is here"); + } } else if (oldglobal != NULL_TREE && (VAR_P (oldglobal) /* If the old decl is a type decl, only warn if the old decl is an explicit typedef or if both the