This is the mail archive of the gcc-patches@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: [PATCH 3.4] Warn for always-false bitwise ands


On Sat, Mar 22, 2003 at 12:36:59AM +0000, John Levon wrote:
> > First of all, a warning like "bitwise and expression is always false"
> > should be on always, much like we do with "comparison is always false
> > due to limited range of data type".
> 
> The latter is much much more likely to be an error than the former,
> IMHO. I can conceive of someone using defines and invoking the former
> warning, but not the latter - it's like if (0) ...

On the contrary.  The later is easy to generate with opaque data types;
the former requires someone to use &, and I'm not sure how one could
"legitimately" create such a case with defines.

> > Second, the warning is incorrect for any constant with low bit set,
> > not just 1.  However, I'm willing to allow that this is likely to
> > be a mistake -- even with 1.  About this I'm not sure what to do.
> 
> I guess I don't understand this. What source code changes do you want me
> to make ?

Let's see...

> > I'm thinking an unconditional warning "suggest && instead of &
> > with booleans".
> 
> This is obviously wrong: 90% of the problems are parentheses issues,
> i.e. if (!a & 0x4) vs. if (!(a & 0x4))

With ! is it almost certainly wrong, as you say.  This is not
true in general with all booleans.  Consider someone wanting to
"optimize" by avoiding the short-circut characteristics of &&.
Consider if this happens with

	(TARGET_DOIT_P & (x != 0))

where TARGET_DOIT_P expands to 0.

Perhaps what you really want to do is notice (!X & C) rather
than all booleans in general.


r~


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