On Sun, 16 Oct 2016, Philip Guenther wrote:
> So let's fix that and make our gcc a bit more like new ones.  Written 
> without peeking at the new ones and tested against the .c file at bottom 
> to verify that it doesn't fail or crash on some weird combo of 
> shadowing.
> 
> oks?

Updated diff that *keeps* the shadow warning for a function parameter 
which is a pointer-to-function and which shadows a global function, e.g.:

#include <err.h>
void
foo( void (*err)(void) )
{
        err();
}

Previous diff suppressed the warning on the 'err' parameter; this revised 
diff keeps it.

oks?

Philip

Index: gnu/gcc/gcc/c-decl.c
===================================================================
RCS file: /cvs/src/gnu/gcc/gcc/c-decl.c,v
retrieving revision 1.4
diff -u -p -r1.4 c-decl.c
--- gnu/gcc/gcc/c-decl.c        10 Sep 2015 10:56:35 -0000      1.4
+++ gnu/gcc/gcc/c-decl.c        17 Oct 2016 18:18:36 -0000
@@ -1946,8 +1946,20 @@ warn_if_shadowing (tree new_decl)
          warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
                   new_decl);
        else if (DECL_FILE_SCOPE_P (old_decl))
-         warning (OPT_Wshadow, "declaration of %q+D shadows a global "
-                  "declaration", new_decl);
+         {
+           /* Don't warn about shadowing a global function unless the local
+              variable or parameter is a pointer to a function */
+           if (TREE_CODE (old_decl) == FUNCTION_DECL
+               && TREE_CODE (new_decl) != FUNCTION_DECL
+               && ((TREE_CODE (new_decl) != VAR_DECL
+                    && TREE_CODE (new_decl) != PARM_DECL)
+                   || !POINTER_TYPE_P (TREE_TYPE (new_decl))
+                   || TREE_CODE (TREE_TYPE (TREE_TYPE (new_decl)))
+                      != FUNCTION_TYPE))
+             break;
+           warning (OPT_Wshadow, "declaration of %q+D shadows a global "
+                    "declaration", new_decl);
+         }
        else if (TREE_CODE (old_decl) == FUNCTION_DECL
                 && DECL_BUILT_IN (old_decl))
          {

Reply via email to