On 17/09/18 14:00, 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;
> 
> typecast the expression like "n& = (char)~n1" and warning goes away .
> 
> and when we investigated the gcc source and warning coming from
> unsafe_conversion_p@ gcc/c-family/c-common.c:1226
> 
> if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
> give_warning = UNSAFE_OTHER;
> 
> where TYPE_PRECISION (type) is 8  for char and TYPE_PRECISION
> (expr_type) is 32  as expected for int .
> 
> is that expected behavior of gcc ?
> 
> clang compiles with no warnings .
> 

This should really be on the gcc-help mailing list, rather than the
development list.

You could argue that in this particular case, since "n" ends up as 0,
the conversion from int to char will not change its value.  But in a
more general case the warning is valid.  "~n1" is done as an "int"
operation (that's the way C works), and the compiler is warning you that
you might be dealing with values too big to fit in the type "char".

gcc can help warn you about suspicious code that might be indicate a bug
- such as this case.  If you don't want this kind of warning, don't
enable it.  But if you /do/ enable the warning, pay attention to it -
your code here is certainly questionable.  (For one thing, it is a good
rule that you should never use bitwise operators with signed types, and
you should never use a naked "char" with arithmetic or bitwise operations.)


Reply via email to