llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.87k stars 11.93k forks source link

clang-tidy: use of signed integer operand with binary bitwise #72917

Open MaJerle opened 11 months ago

MaJerle commented 11 months ago
void
my_func(uint16_t hex, char* ascii) {
    for (uint8_t idx = 4U; idx != 0; --idx) {
        uint8_t aux = (hex >> (4U * (idx - 1U))) & 0x0FU;

        aux = (aux <= 9U) ? (aux + 0x30U) : (aux + 0x57U);
        ascii[4U - idx] = (char)aux;
    }
    ascii[4] = '\0';
}

returns use of a signed integer operand with a biary bitwise operator and points to line uint8_t aux = (hex >> (4U * (idx - 1U))) & 0x0FU;

Adding (uint8_t) cast infront, solves the issue.

This is part of hicpp-signed-bitwise

Version 17.0.5

Why? Doesn't tidy understand it is all unsigned already?

PiotrZSL commented 11 months ago
    |           |   `-BinaryOperator <col:24, col:47> 'int' '>>'
    |           |     |-ImplicitCastExpr <col:24> 'int' <IntegralCast>
    |           |     | `-ImplicitCastExpr <col:24> 'uint16_t':'unsigned short' <LValueToRValue>
    |           |     |   `-DeclRefExpr <col:24> 'uint16_t':'unsigned short' lvalue ParmVar 0x564691aef6f0 'hex' 'uint16_t':'unsigned short'
    |           |     `-ParenExpr <col:31, col:47> 'unsigned int'
    |           |       `-BinaryOperator <col:32, col:46> 'unsigned int' '*'
    |           |         |-IntegerLiteral <col:32> 'unsigned int' 4
    |           |         `-ParenExpr <col:37, col:46> 'unsigned int'
    |           |           `-BinaryOperator <col:38, col:44> 'unsigned int' '-'
    |           |             |-ImplicitCastExpr <col:38> 'unsigned int' <IntegralCast>
    |           |             | `-ImplicitCastExpr <col:38> 'uint8_t':'unsigned char' <LValueToRValue>
    |           |             |   `-DeclRefExpr <col:38> 'uint8_t':'unsigned char' lvalue Var 0x564691aef970 'idx' 'uint8_t':'unsigned char'
    |           |             `-IntegerLiteral <col:44> 'unsigned int' 1

There is implicit cast to int because its uint32 vs uint16 and uint16 is casted to int.

MaJerle commented 11 months ago

is the warning expected and therefore not considered bug?