c++: Don't emit repeated -Wshadow warnings for templates/ctors [PR104379]

Message ID 20220208082218.GD2646553@tucnak
State New
Headers
Series c++: Don't emit repeated -Wshadow warnings for templates/ctors [PR104379] |

Commit Message

Jakub Jelinek Feb. 8, 2022, 8:22 a.m. UTC
  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  <jakub@redhat.com>

	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
  

Comments

Jason Merrill Feb. 8, 2022, 7:14 p.m. UTC | #1
On 2/8/22 03:22, Jakub Jelinek wrote:
> 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?

OK.

> 2022-02-08  Jakub Jelinek  <jakub@redhat.com>
> 
> 	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.
> 
> --- 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<typename T>
> +struct S
> +{
> +  int i;
> +  S(int i) { (void) i; }			// { dg-warning "declaration of 'i' shadows a member of 'S<T>'" }
> +  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<int> i(1);
> +S<long> j(1);
> +S<int> k(1.0f);
> +S<long> l(1.0f);
> +S<int> m(&x);
> +S<int> n(&x);
> 
> 	Jakub
>
  

Patch

--- 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<typename T>
+struct S
+{
+  int i;
+  S(int i) { (void) i; }			// { dg-warning "declaration of 'i' shadows a member of 'S<T>'" }
+  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<int> i(1);
+S<long> j(1);
+S<int> k(1.0f);
+S<long> l(1.0f);
+S<int> m(&x);
+S<int> n(&x);