cat > main.c int i; int main(void) { int i = 0; return i; } ^D $ gcc -Wshadow main.c main.c: In function 'main': main.c:5:7: warning: declaration of 'i' shadows a global declaration [-Wshadow] main.c:1:5: warning: shadowed declaration is here [-Wshadow]
This is a one liner that would qualify as obvious and it won't require copyright assignment. Feel free to submit a patch! (but it probably requires to adjust testcases, so run the testsuite and look for new failures)
Hi, there is an ambiguity here, consider the following examples: $ cat > 1.c int main(void) { int i; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wshadow" { int i; } #pragma GCC diagnostic pop } $ gcc46 -Wshadow 1.c 1.c: In function 'main': 1.c:4:6: warning: shadowed declaration is here [-Wshadow] Here I would suppose to see no warning at all, because I have turned it off for the case that shadows. But: $ cat > 2.c int main(void) { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wshadow" int i; #pragma GCC diagnostic pop {int i;} } $ gcc46 -Wshadow 2.c 2.c: In function 'main': 2.c:9:7: warning: declaration of 'i' shadows a previous local [-Wshadow] And it is something that I whould also not expect. And now: $ cat > 3.c int main(void) { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wshadow" int i; #pragma GCC diagnostic pop #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wshadow" { int i; } #pragma GCC diagnostic pop } $ gcc46 -Wshadow 3.c Produces no warning. So for me it is a bit confusing, since the warning setting refers to pieces of code and not to variables.
(In reply to comment #2) > Produces no warning. So for me it is a bit confusing, since the warning setting > refers to pieces of code and not to variables. You are right and it is a bug. The reason is that each warning message (the second should be note) is produced by different calls to warning(). These calls are conditional on Wshadow but the #pragmas only disable Wshadow for a certain location. The fix is to make "shadowed declaration is here" a call to inform() (that is, a note) conditional on the first warning being emitted, which one can test by checking the return value of warning(). Patches welcome.
There is a patch in PR52116
Mine.
Author: mpolacek Date: Thu Jun 5 05:30:39 2014 New Revision: 211254 URL: http://gcc.gnu.org/viewcvs?rev=211254&root=gcc&view=rev Log: PR c/48062 * c-decl.c (warn_if_shadowing): Call inform instead of warning_at. Print note only if the warning was printed. * gcc.dg/Wshadow-1.c: Use dg-message for "shadowed declaration". * gcc.dg/Wshadow-3.c: Likewise. * gcc.dg/pr48062.c: New test. Added: trunk/gcc/testsuite/gcc.dg/pr48062.c Modified: trunk/gcc/c/ChangeLog trunk/gcc/c/c-decl.c trunk/gcc/testsuite/ChangeLog trunk/gcc/testsuite/gcc.dg/Wshadow-1.c trunk/gcc/testsuite/gcc.dg/Wshadow-3.c
Fixed.