re PR c/529 (-Wshadow warns on function prototypes vs. global vars)
authorJoseph Myers <joseph@codesourcery.com>
Fri, 29 Jul 2005 23:58:37 +0000 (00:58 +0100)
committerJoseph Myers <jsm28@gcc.gnu.org>
Fri, 29 Jul 2005 23:58:37 +0000 (00:58 +0100)
PR c/529
* c-decl.c (warn_if_shadowing): Don't check for PARM_DECL in
nested function declarators.
(pushdecl): Don't call warn_if_shadowing for PARM_DECL.
(grokparms): Call warn_if_shadowing for parameters used within the
parameter list.
(store_parm_decls_newstyle): Call warn_if_shadowing for parameters
not used within the parameter list.
(store_parm_decls_oldstyle): Call warn_if_shadowing for parameters.

testsuite:
* gcc.dg/Wshadow-3.c: New test.

From-SVN: r102571

gcc/ChangeLog
gcc/c-decl.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/Wshadow-3.c [new file with mode: 0644]

index ef5a77457e28f32d3d18753f44871d81dec73a52..c9437b82aa766d5311532875acfe9ec77c376601 100644 (file)
@@ -1,3 +1,15 @@
+2005-07-29  Joseph S. Myers  <joseph@codesourcery.com>
+
+       PR c/529
+       * c-decl.c (warn_if_shadowing): Don't check for PARM_DECL in
+       nested function declarators.
+       (pushdecl): Don't call warn_if_shadowing for PARM_DECL.
+       (grokparms): Call warn_if_shadowing for parameters used within the
+       parameter list.
+       (store_parm_decls_newstyle): Call warn_if_shadowing for parameters
+       not used within the parameter list.
+       (store_parm_decls_oldstyle): Call warn_if_shadowing for parameters.
+
 2005-07-30  Jan Hubicka  <jh@suse.cz>
 
        * expr.c (expand_expr_real_1): Do not load mem targets into register.
index 71a955380eca54c1f66c1da15991acfbc4561f2e..09c2755b768e10abd800d991f95a802c45ae9c36 100644 (file)
@@ -1891,13 +1891,7 @@ warn_if_shadowing (tree new_decl)
       /* No shadow warnings for internally generated vars.  */
       || DECL_IS_BUILTIN (new_decl)
       /* No shadow warnings for vars made for inlining.  */
-      || DECL_FROM_INLINE (new_decl)
-      /* Don't warn about the parm names in function declarator
-        within a function declarator.  It would be nice to avoid
-        warning in any function declarator in a declaration, as
-        opposed to a definition, but there is no way to tell
-        it's not a definition at this point.  */
-      || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
+      || DECL_FROM_INLINE (new_decl))
     return;
 
   /* Is anything being shadowed?  Invisible decls do not count.  */
@@ -2221,7 +2215,8 @@ pushdecl (tree x)
        }
     }
 
-  warn_if_shadowing (x);
+  if (TREE_CODE (x) != PARM_DECL)
+    warn_if_shadowing (x);
 
  skip_external_and_shadow_checks:
   if (TREE_CODE (x) == TYPE_DECL)
@@ -4836,6 +4831,9 @@ grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
                             parm, parmno);
                }
            }
+
+         if (DECL_NAME (parm) && TREE_USED (parm))
+           warn_if_shadowing (parm);
        }
       return arg_types;
     }
@@ -6106,8 +6104,12 @@ store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
     {
       DECL_CONTEXT (decl) = current_function_decl;
       if (DECL_NAME (decl))
-       bind (DECL_NAME (decl), decl, current_scope,
-             /*invisible=*/false, /*nested=*/false);
+       {
+         bind (DECL_NAME (decl), decl, current_scope,
+               /*invisible=*/false, /*nested=*/false);
+         if (!TREE_USED (decl))
+           warn_if_shadowing (decl);
+       }
       else
        error ("%Jparameter name omitted", decl);
     }
@@ -6181,6 +6183,7 @@ store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
              DECL_ARG_TYPE (decl) = integer_type_node;
              layout_decl (decl, 0);
            }
+         warn_if_shadowing (decl);
        }
       /* If no declaration found, default to int.  */
       else
@@ -6189,6 +6192,7 @@ store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
          DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
          DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
          pushdecl (decl);
+         warn_if_shadowing (decl);
 
          if (flag_isoc99)
            pedwarn ("type of %q+D defaults to %<int%>", decl);
index 936ff524a8f612a3d54dfdc4b26cd5e3ee53aa50..6e1004c67ef4985e5264a862dacae1a6c68fdfa2 100644 (file)
@@ -1,3 +1,8 @@
+2005-07-29  Joseph S. Myers  <joseph@codesourcery.com>
+
+       PR c/529
+       * gcc.dg/Wshadow-3.c: New test.
+
 2005-07-29  Thomas Koenig  <Thomas.Koenig@online.de>
 
        * gfortran.dg/matmul_1.f90:  Correct LHS of matmul test (it
diff --git a/gcc/testsuite/gcc.dg/Wshadow-3.c b/gcc/testsuite/gcc.dg/Wshadow-3.c
new file mode 100644 (file)
index 0000000..4d1924a
--- /dev/null
@@ -0,0 +1,21 @@
+/* Test warnings for shadowing in function prototype scope: generally
+   useless but of use if the parameter is used within the scope.  Bug
+   529.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu89 -Wshadow" } */
+
+int v; /* { dg-warning "shadowed declaration" } */
+int f1(int v);
+int f2(int v, int x[v]); /* { dg-warning "warning: declaration of 'v' shadows a global declaration" } */
+int f3(int v, int y[sizeof(v)]); /* { dg-warning "warning: declaration of 'v' shadows a global declaration" } */
+int f4(int v) { return 0; } /* { dg-warning "warning: declaration of 'v' shadows a global declaration" } */
+int f5(int v, int x[v]) { return 0; } /* { dg-warning "warning: declaration of 'v' shadows a global declaration" } */
+int f6(int x) { return 0; }
+int f7(v) int v; { return 0; } /* { dg-warning "warning: declaration of 'v' shadows a global declaration" } */
+int f8(v, w) int v; int w[v]; { return 0; } /* { dg-warning "warning: declaration of 'v' shadows a global declaration" } */
+int f9(x) int x; { return 0; }
+int f10(v) { return 0; } /* { dg-warning "warning: declaration of 'v' shadows a global declaration" } */
+int f11(int a, int b(int a));
+int f12(int a, int b(int a, int x[a])); /* { dg-warning "warning: declaration of 'a' shadows a parameter" } */
+/* { dg-warning "shadowed declaration" "outer parm" { target *-*-* } 20 } */