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: named warnings & individual warning control


I wrote:
> >...  The most common false positive for uninitialized variable
> >warnings takes this form:
> >
> >    T* ptr_T;
> >    bool ptr_is_valid = false;
> >    if (some_fancy_test()) {
> >	ptr_T = foo();
> >	ptr_is_valid = true;
> >    }
> >    do_something_else();
> >    if (ptr_is_valid) {
> >	do_something_with(ptr_T);
> >    }

On Fri, Jun 25, 2004 at 06:12:50PM -0700, Stan Shebs wrote:
> Two answers come to mind:
> 
>     T* ptr_T = NULL;
> 
> If this default initialization is provably unnecessary, then it
> will be removed, right? If it's not redundant, then a good thing
> it was there. This will take some user education before they will
> believe it gets removed.

The current compiler (even the trunk, with tree-ssa) is not smart enough
to remove the initialization, so evidently it's not the users who need to
be educated on that point. :-) As I said, you pretty much need something
like gated SSA form to know that the "uninitialized definition" cannot
reach the argument of do_something_with.

In practice, this wasted initialization only matters in expensive inner
loops; otherwise, it's advisable to put it in.  But it is important to
understand that GCC is not smart enough to optimize it away, and still
will not be smart enough even with tree-ssa.

> Or,
> 
>     T* ptr_T __attribute__((I_know_what_Im_doing));

OK for people whose code only needs to compile with gcc; otherwise
some macro needs to be used, with appropriate definitions for each
target compiler.

It so happened that, due to a bug, the compiler used to remain silent if
the user wrote something like

	T* ptr_T = ptr_T;

and a few developers were relying on this "feature" and argued against
fixing it.  But the result was that some real bugs were being missed.



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