On 2018-09-17 10:03:48 -0600, Martin Sebor wrote:
> On 09/17/2018 06:00 AM, Umesh Kalappa wrote:
> > Hi All,
> >
> > When we try to compile the below case from trunk gcc we get the below
> > warning (-Wconversion) i.e
> >
> > void start(void) {
> > char n = 1;
> > char n1 = 0x01;
> > n &= ~n1;
> > }
> >
> > $xgcc -S warn.c -nostdinc -Wconversion
> > warning: conversion from ‘int’ to ‘char’ may change value [-Wconversion]
> > n &= ~n1;
[...]
> It looks like a bug to me.
>
> Declaring n1 const avoids the warning at -O2 but in C but not
> at -O0.
Perhaps at some optimization level, GCC determines that the
expression is safe (thus no longer emits the warning), i.e.
that n & ~n1 is necessarily representable in a char.
> That doesn't seem quite right -- GCC determines the
> type of the bitwise AND expression to be different between
> the optimization levels.
No, the type of this AND expression is always int. The question
is whether this int is necessarily representable in a char.
> In C++, declaring n1 const avoids the warning regardless of
> optimization levels.
If the constant propagation is done at -O0, this could explain
the behavior.
Or do you mean that GCC remembers the type the data come from,
i.e. assuming char is signed, if n1 is of type char, then ~n1
is necessarily representable in a char, thus can be regarded
as of being of type char in its analysis?
--
Vincent Lefèvre <[email protected]> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)