Bug 48062 - `shadowed declaration is here' should be a note
Summary: `shadowed declaration is here' should be a note
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.6.0
: P3 trivial
Target Milestone: 5.0
Assignee: Marek Polacek
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2011-03-10 16:42 UTC by Dmitry Gorbachev
Modified: 2014-06-05 06:21 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-03-10 18:44:34


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dmitry Gorbachev 2011-03-10 16:42:17 UTC
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]
Comment 1 Manuel López-Ibáñez 2011-03-10 18:44:34 UTC
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)
Comment 2 momchil 2011-06-09 01:46:43 UTC
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.
Comment 3 Manuel López-Ibáñez 2011-06-09 07:55:37 UTC
(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.
Comment 4 Manuel López-Ibáñez 2012-02-13 17:25:11 UTC
There is a patch in PR52116
Comment 5 Marek Polacek 2014-06-03 16:57:19 UTC
Mine.
Comment 6 Marek Polacek 2014-06-05 05:31:11 UTC
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
Comment 7 Marek Polacek 2014-06-05 06:21:16 UTC
Fixed.