This is the mail archive of the gcc-bugs@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]

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


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)

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