This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] -Wredundant-decls, do not warn for static forward decl


Hi,

The FreeBSD kernel has code such as this which compiles with
gcc 3.4:

extern struct domain routedomain;               /* or at least forward */

static struct protosw routesw[] = {
{ SOCK_RAW,     &routedomain,   0,              PR_ATOMIC|PR_ADDR,
  0,            route_output,   raw_ctlinput,   0,
  0,
  raw_init,     0,              0,              0,
  &route_usrreqs
}
};

static struct domain routedomain =
    { PF_ROUTE, "route", 0, 0, 0,
      routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };



This of course is illegal ISO C, and gcc 4.0 will correctly
issue an error message:
error: static declaration of 'routedomain' follows non-static declaration

Changing this could to be:
static struct domain routedomain;               /* or at least forward */

will compile without error, but will emit a warning
with -Wredundant-decls:
warning: redundant redeclaration of 'routedomain'


I had a discussion on the freebsd-arch mailing list with the
FreeBSD developers on how to fix this problem:

http://lists.freebsd.org/pipermail/freebsd-arch/2005-August/004181.html

Basically, the result of the discussion was that
code which is illegal ISO C should be fixed, but it would be
nice to fix GCC so that -Wredundant-decls does not warn for static forward 
declarations.  -Wredundant-decls in other cases emits useful warnings,
but in this case, the warning is not useful.

Would the following patch to c-decl.c be acceptable?

2005-8-27  Craig Rodrigues  <rodrigc@gcc.gnu.org>

        * c-decl.c (diagnose_mismatched_decls):  With -Wredundant-decls,
        do not issue warning for static forward declaration of static variables.


Index: c-decl.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/c-decl.c,v
retrieving revision 1.630.6.16
diff -u -u -r1.630.6.16 c-decl.c
--- c-decl.c	16 Aug 2005 20:34:19 -0000	1.630.6.16
+++ c-decl.c	27 Aug 2005 23:43:06 -0000
@@ -1559,7 +1559,10 @@
       && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
       /* Don't warn about forward parameter decls.  */
       && !(TREE_CODE (newdecl) == PARM_DECL
-	   && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
+	   && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
+      /* Don't warn about forward static variable decls.  */
+      && !(TREE_CODE (newdecl) == VAR_DECL
+	   && !TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl)))
     {
       warning ("%Jredundant redeclaration of %qD", newdecl, newdecl);
       warned = true;





-- 
Craig Rodrigues        
rodrigc@crodrigues.org


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]