This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: a warning to implement


> 
> <<I've only proposed it as a feature.  I don't think this is
> documented as such.
> >>
> 
> So no one could actually be using 
> 
> int a = a;
> 
> for any useful purpose right now as I understand it, so I am at a complete
> loss to understand Gaby's claim that some people think this is useful.

OK, here's the scoop.

Someone evidently discovered that if one writes "int a = a" it suppresses
uninitialized variable warnings!  This seems to be an accidental feature
of gcc, arguably a misfeature, and now that I see it I understand why
gcc failed to warn about coding errors I've made in the past.

The reason people seem to be motivated to do things like this has to do
with weaknesses in the current uninitialized variable analysis.  For
example

void fred(int);

int foo(int a, int b)
{
    int bar;
    int flag = 0;
    if (a > b) {
	bar = a;	/* this is the only place bar is set */
	flag = 1;
    }
    if (flag) {
	fred(bar);	/* this is the only place bar is used */
    }
    return 0;
}

% gcc -O -Wall -c foo.c
foo.c: In function `foo':
foo.c:5: warning: `bar' might be used uninitialized in this function

We get the warning because gcc is not smart enough to trace the value of
the flag variable and figure out that fred will only be called in cases
where bar is set.

But if the first line of the body of foo is changed to

    int bar = bar;

the warning goes away, and furthermore, the optimizer will remove the
assignment, so the user gets slightly better code than they would have
if they had written

    int bar = 0;

because the same weakness that gives us a bad warning keeps gcc from
knowing that the assignment of 0 is dead code.

It appears that some people have discovered this and are using it in
production code.  IMHO, this sucks.  We should provide some cleaner
way of suppressing the warning.

In fact "int a = a" uses an uninitialized variable and should give
warnings according to *current documentation* when -Wall is given, but
evidently Gaby wants us to preserve this botch indefinitely!  Better
to provide some kind of lint-like comment or attribute to tell gcc
to shut up about alleged uninitialized use.

Alternatively, I'd like to require the user to explicitly say that s/he
is intentionally using this warning-suppressing idiom, by saying something
like
	-Wno-self-assign




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]