Add -Wshadow=global -Wshadow=local and -Wshadow=compatible-local.
authorLe-Chun Wu <lcwu@google.com>
Mon, 31 Oct 2016 13:21:59 +0000 (13:21 +0000)
committerMark Wielaard <mark@gcc.gnu.org>
Mon, 31 Oct 2016 13:21:59 +0000 (13:21 +0000)
commit84ff4775d41b716c1b862d4d91ff69127686b668
tree37de03a0d419a258e63aa73193cee37e6d221b31
parent651795857809d5e504aa577c3912b98f4feaf397
Add -Wshadow=global -Wshadow=local and -Wshadow=compatible-local.

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
15 files changed:
gcc/ChangeLog
gcc/c/ChangeLog
gcc/c/c-decl.c
gcc/common.opt
gcc/cp/ChangeLog
gcc/cp/name-lookup.c
gcc/doc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wshadow-compatible-local-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wshadow-local-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wshadow-local-2.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wshadow-compatible-local-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wshadow-local-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wshadow-local-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wshadow-local-3.c [new file with mode: 0644]