unsigned / double propagation weirdness?

Joe Buck Joe.Buck@synopsys.COM
Mon Feb 23 19:40:00 GMT 2004


[ warn about unary minus on unsigned? ]

On Mon, Feb 23, 2004 at 06:00:26PM +0100, Richard Guenther wrote:
> Well, if it's not in -Wall, nobody will actually use it and it will not 
> help finding bugs.  The warning should probably be only, if inside
> a mixed signed/unsigned expression (where float counts as signed of 
> course).  I.e. for completely unsigned expression we should not warn.

If unary_minus(unsigned) is then converted to a signed integer of the
same size, there is no problem; the mathematically expected result occurs.
There is only a problem if it is converted to a wider signed type, or
a floating type.  The same goes for binary minus of two unsigned args.
Example:

unsigned u;
int i;
long l;
...

	i = -u; /* no problem */
	l = -u; /* problem, if sizeof(long) > sizeof(int) */

So the warning would only be generated if an unsigned negation is
widened (or potentially widened, in the case of conversions to long,
to alert the programmer to portability issues).

This should eliminate Robert's concerns about intentional use of -
for two's complement: in such cases, the result would not be widened.



More information about the Gcc mailing list