[Bug c/67764] -Wconversion generates false warnings for bitmask+cast expressions

egall at gwmail dot gwu.edu gcc-bugzilla@gcc.gnu.org
Tue Sep 29 19:57:00 GMT 2015


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67764

Eric Gallager <egall at gwmail dot gwu.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |egall at gwmail dot gwu.edu

--- Comment #1 from Eric Gallager <egall at gwmail dot gwu.edu> ---
Remember, x |= y is the same as x = (x | y). So if we rewrite the problematic
conversions like that, it becomes clearer where the warning is really pointing:

$ /usr/local/bin/gcc -c conversion_bug.c -Wconversion
conversion_bug.c: In function ‘_setbit’:
conversion_bug.c:4:13: warning: conversion to ‘unsigned char’ from ‘int’ may
alter its value [-Wconversion]
  b[i / 8] = (b[i / 8] | (unsigned char)(1 << (i % 8)));
             ^
conversion_bug.c: In function ‘_mask_stupid’:
conversion_bug.c:15:9: warning: conversion to ‘unsigned char’ from ‘int’ may
alter its value [-Wconversion]
  b[i] = (b[i] | (unsigned char)i);
         ^

If you cast the entire expression after it has been re-written like that, the
warning goes away:

b[i / 8] = (unsigned char)(b[i / 8] | (unsigned char)(1 << (i % 8)));
b[i] = (unsigned char)(b[i] | (unsigned char)i);

(no warnings generated for either of those)


More information about the Gcc-bugs mailing list