This is the mail archive of the 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: warning: conversion from ‘int’ to ‘char’ may change value

On 2018-09-20 22:57:00 +0800, Liu Hao wrote:
> 在 2018/9/20 22:08, Vincent Lefevre 写道:
> >> 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?
> > 
> In C++ adding the `const` qualifier makes `~n1` a constant expression. 
> In C it never is, regardless of qualifiers.

What I meant is that since n1 is a constant, its value is known,
and GCC can deduce the value of ~n1 (this is not a constant
expression, but its value is known at compile time, which is
what matters here).

In the code here, even if n1 is not declared as const, the value
of ~n1 can also be deduced at compile time, but this requires code
analysis to detect whether n1 could have been modified. Thus this
is more complex.

> BTW, I am quite disappointed with such 'false' warnings, because by 
> performing a compound AND-and-ASSIGN operation on a `char` object I have 
> no interest in bits that don't fit into a `char`, be they ones or 
> zeroes. Perhaps there are scenarios where they shouldn't be ignored, but 
> I can't think of any.

Then you can disable the warning, or use optimization to enable
further code analysis (but by doing that, you can get new false
positives). In doubt, GCC cannot guess what you have in mind in

Vincent Lefèvre <> - Web: <>
100% accessible validated (X)HTML - Blog: <>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)

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