Bug 96747 - -Wshadow accepts included extern variable shadowing
Summary: -Wshadow accepts included extern variable shadowing
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 10.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Wshadow
  Show dependency treegraph
 
Reported: 2020-08-22 16:49 UTC by Martin Matous
Modified: 2022-06-03 02:40 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
-save-temps *.i output (7.04 KB, text/plain)
2020-08-22 16:49 UTC, Martin Matous
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Matous 2020-08-22 16:49:11 UTC
Created attachment 49098 [details]
-save-temps *.i output

When I compile the attached code with 
gcc -Wshadow ./gcc-shadow-test.c
it passes with no output/warnings. The warning about shadowing is displayed properly by g++, clang and clang++ and the first print() actually proves that gcc does see the "environ" variable from unistd.h but ignores it when resolving shadowing.

gcc -v:
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /var/tmp/portage/sys-devel/gcc-10.2.0/work/gcc-10.2.0/configure --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/10.2.0 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/10.2.0 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/10.2.0/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/10.2.0/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include/g++-v10 --with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/10.2.0/python --enable-languages=c,c++,fortran --enable-obsolete --enable-secureplt --disable-werror --with-system-zlib --enable-nls --without-included-gettext --enable-checking=release --with-bugurl=https://bugs.gentoo.org/ --with-pkgversion='Gentoo 10.2.0 p1' --disable-esp --enable-libstdcxx-time --with-build-config=bootstrap-lto --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-multilib --with-multilib-list=m32,m64 --disable-fixed-point --enable-targets=all --enable-libgomp --disable-libssp --disable-libada --disable-systemtap --enable-vtable-verify --with-zstd --enable-lto --without-isl --enable-default-pie --enable-default-ssp
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.0 (Gentoo 10.2.0 p1)
Comment 1 Harald van Dijk 2020-08-23 14:31:26 UTC
This happens because environ is declared in a system header. The C frontend warns when both declarations are not in a system header. The C++ frontend warns when the second declaration is not in a system header.

Intuitively, the C++ frontend's behaviour makes more sense to me, but that is just my personal opinion.

The C frontend's behaviour was changed intentionally, see <https://gcc.gnu.org/legacy-ml/gcc-patches/2011-08/msg02017.html>, but -Wshadow has since been enhanced so that it no longer warns about object declarations shadowing functions. If that later enhancement already covers all of the cases that that initial system headers patch was meant to suppress warnings for, perhaps the initial patch can be reverted now?
Comment 2 Martin Matous 2020-08-23 22:12:37 UTC
Other compiler's behavior does makes more sense, but yeah, personal opinion. In case the patch ends up not being reverted the docs at https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wshadow should probably be clarified about what kind of shadowing is silently ignored.