From patchwork Thu Aug 21 22:01:21 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: 382052 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 2141F140082 for ; Fri, 22 Aug 2014 08:01:55 +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=YwvKGE/APdTl34p tXyV4NbqDnhUU6IjFLNFV/DcJzLtkUyw53T3j7LRS4AWI+yfFXcsyMLZgKcSuduJ xhGkfrVjhEdxrxYyeByfSi9CnmAS+2LPF++9W7qZVmJ+lhlm60g+EeJdKH221iWD zgeGo6F7juq4RIcPOhTvN0izj65Y= 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=G2i5onU4QGaB2hRqgdC8g 9P+IWs=; b=y65pxeClMD9zaiCutZqW7hDlTSfMqVLxrmMsbeQDqrb83CkvcvD86 FvWszyBG9NGwSqJfBYHeEhZvbGXoK6tQBY+YFHE6mX+YOFyHeHA4rvZNye6t6F2s kxJ4x4Wj0EGR68m13qsB9jN/HZBUcw2LbR6uxK91HEF5MGx3jmM4bc= Received: (qmail 8071 invoked by alias); 21 Aug 2014 22:01:47 -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 8060 invoked by uid 89); 21 Aug 2014 22:01:45 -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; Thu, 21 Aug 2014 22:01:44 +0000 Received: by mail-wi0-f176.google.com with SMTP id bs8so9382998wib.3 for ; Thu, 21 Aug 2014 15:01:41 -0700 (PDT) X-Received: by 10.180.73.235 with SMTP id o11mr6864536wiv.41.1408658501898; Thu, 21 Aug 2014 15:01:41 -0700 (PDT) MIME-Version: 1.0 Received: by 10.217.80.73 with HTTP; Thu, 21 Aug 2014 15:01:21 -0700 (PDT) In-Reply-To: <53F66607.5070305@redhat.com> References: <53F66607.5070305@redhat.com> From: =?ISO-8859-1?Q?Manuel_L=F3pez=2DIb=E1=F1ez?= Date: Fri, 22 Aug 2014 00:01:21 +0200 Message-ID: Subject: Re: [PATCH c++/57709] Wshadow is too strict / has false positives To: Jason Merrill Cc: Gcc Patch List X-IsSubscribed: yes 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. Bootstrapping and regression testing in progress. But the testcase works as expected. OK if it passes? gcc/cp/ChangeLog: 2014-08-21 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. gcc/testsuite/ChangeLog: 2014-08-21 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/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 + && !(FUNCTION_POINTER_TYPE_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