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

Geoffrey Keating geoffk@geoffk.org
Fri Sep 2 23:14:00 GMT 2005


Craig Rodrigues <rodrigc@crodrigues.org> writes:

> 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.

I do think that -Wredundant-decls should not warn for forward declarations,
but your patch does more than that.  For instance, your patch will suppress
the warning for

static int x;
static int x;

which is something that -Wredundant-decls should clearly catch.

You can check that something is a forward declaration because the
first one will have no DECL_INITIAL, but the second one will have a
DECL_INITIAL.  I think your patch would be good with this change.



More information about the Gcc-patches mailing list