diff mbox

[c++/57709] Wshadow is too strict / has false positives

Message ID CAESRpQAG-Ev-rSSPyNWST18sG9zJ2brsitRWa910=_nDTWJ8_A@mail.gmail.com
State New
Headers show

Commit Message

Manuel López-Ibáñez Aug. 21, 2014, 8:22 p.m. UTC
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  <manu@gcc.gnu.org>

    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  <manu@gcc.gnu.org>

    PR c++/57709
    * g++.dg/Wshadow.C: New test.

Comments

Jason Merrill Aug. 21, 2014, 9:35 p.m. UTC | #1
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?

Jason
diff mbox

Patch

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