84ff4775d4
This patch from Le-Chun Wu adds two new shadow warning flags for C and C++: -Wshadow=local which warns if a local variable shadows another local variable or parameter, -Wshadow=compatible-local which warns if a local variable shadows another local variable or parameter whose type is compatible with that of the shadowing variable. It is already on the google/main branch (Google ref 39127) and was previously submitted by Diego Novillo and reviewed on http://codereview.appspot.com/4452058 I addressed the review comments and made the following changes: - Add -Wshadow=global (the default alias for -Wshadow). - Make the documented options -Wshadow=global, -Wshadow=local and -Wshadow=compatible-local (with hidden undocumented aliases -Wshadow-local and -Wshadow-compatible-local for compatibility). - The -Wshadow=global, -Wshadow=local and -Wshadow=compatible-local relationships are expressed in common.opt instead of in opts.c and documented in invoke.texi. - The "previous declaration" warnings were turned into notes and use the (now) existing infrastructure instead of duplicating the warnings. The testcases have been adjusted to expect the notes. - The conditional change in name-lookup.c for non-locals (where we don't want to change the warnings, but just check the global ones) has been dropped. - Use warning_at in c-decl.c (warn_if_shadowing). gcc/ChangeLog: 2016-10-30 Le-Chun Wu <lcwu@google.com> Mark Wielaard <mjw@redhat.com> * doc/invoke.texi: Document Wshadow-local and Wshadow-compatible-local. * common.opt (Wshadow=global): New option. Default for -Wshadow. (Wshadow=local): New option. (Wshadow-local): Hidden alias for -Wshadow=local. (Wshadow=compatible-local): New option. (Wshadow-compatible-local): Hidden alias for -Wshadow=compatible-local. * doc/invoke.texi: Document Wshadow=global, Wshadow=local and Wshadow=compatible-local. gcc/c/ChangeLog: 2016-10-30 Le-Chun Wu <lcwu@google.com> Mark Wielaard <mjw@redhat.com> * c-decl.c (warn_if_shadowing): Use the warning code corresponding to the given -Wshadow= variant. Use warning_at. gcc/cp/ChangeLog: 2016-10-30 Le-Chun Wu <lcwu@google.com> Mark Wielaard <mjw@redhat.com> * name-lookup.c (pushdecl_maybe_friend): When emitting a shadowing warning, use the code corresponding to the given -Wshadow= variant. gcc/testsuite/ChangeLog 2016-10-30 Le-Chun Wu <lcwu@google.com> Mark Wielaard <mjw@redhat.com> * gcc.dg/Wshadow-compatible-local-1.c: New test. * gcc.dg/Wshadow-local-1.c: Likewise. * gcc.dg/Wshadow-local-2.c: Likewise. * g++.dg/warn/Wshadow-compatible-local-1.C: Likewise. * g++.dg/warn/Wshadow-local-1.C: Likewise. * g++.dg/warn/Wshadow-local-2.C: Likewise. Co-Authored-By: Mark Wielaard <mjw@redhat.com> From-SVN: r241699
23 lines
438 B
C
23 lines
438 B
C
/* { dg-do compile } */
|
|
/* { dg-options "-Wshadow=local" } */
|
|
|
|
int decl1; /* should not warn */
|
|
void foo (double decl1) /* should not warn */
|
|
{
|
|
}
|
|
|
|
void foo2 (int d) /* { dg-message "shadowed declaration" } */
|
|
{
|
|
{
|
|
double d; /* { dg-warning "shadows a parameter" } */
|
|
}
|
|
}
|
|
|
|
void foo3 ()
|
|
{
|
|
int local; /* { dg-message "shadowed declaration" } */
|
|
{
|
|
int local; /* { dg-warning "shadows a previous local" } */
|
|
}
|
|
}
|