[PATCH] Add a truncation warning for values under a bitwise or

Arthur O'Dwyer arthur.j.odwyer at gmail.com
Fri Aug 2 14:22:46 PDT 2013

On Fri, Aug 2, 2013 at 1:56 PM, Sam Panzer <espanz at gmail.com> wrote on
http://llvm-reviews.chandlerc.com/D405 :
> I disabled those checks, then realized that BO_Xor might cause some false positives too.
>     char c = 0x1ff ^ Y;
> is also fine if Y is between 0x100 and 0x1ff. It's even worse with something like
>     char c = 0x0f00 ^ Y
> where Y is between 0xf080 and 0xf0ff, since the second byte of Y is known to
> start with a 1. I think that truncation leaves the result the same for signed inputs,
> since the result should be between -1 and -256.
> Should I also remove the non-Assign Xor check for now?

FWIW, I disagree; if I ever wrote

    assert(0x100 <= Y && Y <= 0x1ff);
    char c = 0x142 ^ Y;

I would like to be warned about that. The proper way to write that
code would be one of

    char c = 0x42 ^ (char) Y;
    char c = 0x42 ^ Y;

However, unrelatedly, I think you *should* try to avoid a diagnostic
on compile-time constants featuring "~". For example,

    char highbits = 0xffffffe0 | Y;   // should warn
    char highbits = ~0x1f | Y;        // should definitely not warn

my $.02,

More information about the cfe-commits mailing list