Search code examples
cpicxc8

Intermittent conversion warnings when bitwise OR'ing literals in XC8 V1.44


While composing some bitmask values in a XC8 (v1.44) project I found this rather odd behavior:

1:    uint8_t foo;
2:    foo = 0x01;
3:    foo = 0x01 | 0x02;
4:    foo = 0x01 | 0x02 | 0x04;
5:    foo = 0x01 | 0x02 | 0x04 | 0x08;

Lines 3 and 5 emit a warning:

warning: (752) conversion to shorter data type

While line 4 does not. I understand the operands are promoted to int prior to performing the OR operation and that is where the narrowing warning comes from. However, why does it only warn on some lines and not on the others?

Is there some kind of obscure C rule in play or is this just a compiler bug ?


Solution

  • It is just another compiler bug, the PIC compilers are quite infamous.

    On all those lines, an int gets implicitly converted to a uint8_t upon assignment (lvalue conversion). A tool that is not broken would report every line or no line at all.

    I understand the operands are promoted to int

    No, they aren't, since integer literals are always of int type to begin with.