From patchwork Thu Aug 21 20:22:44 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: 382032 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 21BDD14009C for ; Fri, 22 Aug 2014 06:23:16 +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:from:date:message-id:subject:to:content-type; q= dns; s=default; b=SzHtXmt4Y6Wp6bOs6mumpJpGanqjJkyZpj5dtjXRrBW7kK UtPsnnK+AqCuyA1AFIB58b3aY+aiJe/Wp8c6POtxlLL8MEtn5hSGKvERrUMsCZTD pk4xkokd+QFKIei8kZKpM14xU3f6F11F9xlNkUkqQeYcKPWexrus3YHju0DgU= 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:from:date:message-id:subject:to:content-type; s= default; bh=b+7yTcNvxqpR6Q2n/7w5FSD71/I=; b=wJwIl1RZZUcl6dD8La+P 5DriZKXLEtlauWRxcnojRrH6wu3p4pKI8aYEApzEE2usiesT0M6PS9CwBc32WadR ycCgLIVzHMIAWiqehE7wZ6cWfvSFOVLRZGXbSNvsxhtNht+3Ms/j7tBhg6deD8KA 8RvxY1i+vrlewofjdfmXWAk= Received: (qmail 16397 invoked by alias); 21 Aug 2014 20:23:09 -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 16388 invoked by uid 89); 21 Aug 2014 20:23:08 -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-wg0-f51.google.com Received: from mail-wg0-f51.google.com (HELO mail-wg0-f51.google.com) (74.125.82.51) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Thu, 21 Aug 2014 20:23:07 +0000 Received: by mail-wg0-f51.google.com with SMTP id b13so9570693wgh.34 for ; Thu, 21 Aug 2014 13:23:04 -0700 (PDT) X-Received: by 10.180.73.235 with SMTP id o11mr6469181wiv.41.1408652584450; Thu, 21 Aug 2014 13:23:04 -0700 (PDT) MIME-Version: 1.0 Received: by 10.217.80.73 with HTTP; Thu, 21 Aug 2014 13:22:44 -0700 (PDT) From: =?ISO-8859-1?Q?Manuel_L=F3pez=2DIb=E1=F1ez?= Date: Thu, 21 Aug 2014 22:22:44 +0200 Message-ID: Subject: [PATCH c++/57709] Wshadow is too strict / has false positives To: Gcc Patch List , Jason Merrill X-IsSubscribed: yes In GCC 4.8 I implemented: "The option -Wshadow no longer warns if a declaration shadows a function declaration, unless the former declares a function or pointer to function, because this is a common and valid case in real-world code." This patch applies the same heuristic to member functions. Bootstrapped and regression tested on x86_64-linux-gnu. OK? 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 or pointer to 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,12 @@ +// { dg-do compile } +// { dg-options "-Wshadow" } +// PR c++/57709 +class C { + int both_var; // { dg-message "declaration" } + void var_and_method() {} + void m() { + int + both_var, // { dg-warning "shadows" } + var_and_method; + } +}; 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); + gcc_assert(!really_overloaded_fn (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)))) + { + 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