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: -Wconversion bug in g++-4.3?


On 23/11/2007, Joe Buck <Joe.Buck@synopsys.com> wrote:
> >
> > wconversion-3.c:3: warning: conversion to 'unsigned char' from 'int'
> > may alter its value
> > wconversion-3.c:3: warning: conversion to 'short unsigned int' from
> > 'int' may alter its value
>
> The type of (b & 0xff) is int.  However, the value is bounded, and must be
> in 0x00 to 0xff, so the conversion will not alter the value.  I don't know
> if the warning code is structured to take the value range into account.

No, since guessing the value of b seems a middle-end thing, doesn't it?

Nevertheless, that doesn't really matter here since the code is
warning for "(int)b" being converted to 'unsigned char'. (The warning
occurs even when there is no assignment.) This has to do with how
c-typeck.c's build_binary_op builds this operation. If I  correctly
understand the code there, it basically builds something like

int(unsigned char(int(b)) & unsigned char(0xff))

The conversion from int to unsigned char happens at a point where no
user-provided cast can reach. Luckily, Jakub seems to have a a fix to
work around this in the warning code by using get_narrower to see if
the expression being converted is zero or sign extended of some
smaller type. If the conversion from this type to the target type
can't alter its value, nor change sign, then we shouldn't warn.

Cheers,

Manuel.


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