| Issue |
234217
|
| Summary |
[Clang] Inconsistent -Wshift-count-overflow for narrow compound shifts after integer promotion
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
matt-gretton-dann
|
This issue was found by Codex, I have written the report.
AArch64 targeting Clang trunk inconsistently warns on `>>` and `>>=` on `unsigned char` and `unsigned short` if the shift was at the lvalue's 8-/16-bit storage width. `>>=` receives a warning, an equivalent `>>` does not.
This is an incorrect warning issue only. I do not think there is a code generation error.
## Reproducer
Save as `shift-count.c`.
```c
unsigned char char_compound(unsigned char x) { x >>= 8; return x; }
unsigned char char_ordinary(unsigned char x) { x = x >> 8; return x; }
unsigned short short_compound(unsigned short x) { x >>= 16; return x; }
unsigned short short_ordinary(unsigned short x) { x = x >> 16; return x; }
#ifdef INCLUDE_INVALID_COUNTS
unsigned char count_32(unsigned char x) { x >>= 32; return x; }
unsigned char count_negative(unsigned char x) { x >>= -1; return x; }
#endif
```
Compile:
```sh
clang --no-default-config -target aarch64-unknown-linux-gnu \
-ffreestanding -x c -std=c17 -Wall -Wextra -Werror \
-ferror-limit=0 -fsyntax-only shift-count.c
```
C++ exhibits same issue.
The command exits 1 with only these messages:
```text
shift-count.c:1:50: error: shift count >= width of type [-Werror,-Wshift-count-overflow]
1 | unsigned char char_compound(unsigned char x) { x >>= 8; return x; }
| ^ ~
shift-count.c:3:53: error: shift count >= width of type [-Werror,-Wshift-count-overflow]
3 | unsigned short short_compound(unsigned short x) { x >>= 16; return x; }
| ^ ~~
2 errors generated.
```
The warnings point to `x >>= 8` and `x >>= 16`; neither ordinary assignment is warned about. Compiling with `-INCLUDE_INVALID_COUNTS` also diagnoses `>>= 32` and `>>= -1`, as expected.
## Expected behaviour
My reading of the standard (both C & C++) is that these are valid. `x >>= 8` and `x >>= 16` warnings should not occur.
In any case the compiler should be consistent between `x >> = 8` and `x = x >> 8`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs