Commit f0dcf32
authored
[Sema] Fix tautological bounds check warning with -fwrapv (#120480)
The tautological bounds check warning added in #120222 does not take into account whether signed integer overflow is well defined or not, which could result in a developer removing a bounds check that may not actually be always false because of different overflow semantics. ```c int check(const int* foo, unsigned int idx) { return foo + idx < foo; } ``` ``` $ clang -O2 -c test.c test.c:3:19: warning: pointer comparison always evaluates to false [-Wtautological-compare] 3 | return foo + idx < foo; | ^ 1 warning generated. # Bounds check is eliminated without -fwrapv, warning was correct $ llvm-objdump -dr test.o ... 0000000000000000 <check>: 0: 31 c0 xorl %eax, %eax 2: c3 retq ``` ``` $ clang -O2 -fwrapv -c test.c test.c:3:19: warning: pointer comparison always evaluates to false [-Wtautological-compare] 3 | return foo + idx < foo; | ^ 1 warning generated. # Bounds check remains, warning was wrong $ llvm-objdump -dr test.o 0000000000000000 <check>: 0: 89 f0 movl %esi, %eax 2: 48 8d 0c 87 leaq (%rdi,%rax,4), %rcx 6: 31 c0 xorl %eax, %eax 8: 48 39 f9 cmpq %rdi, %rcx b: 0f 92 c0 setb %al e: c3 retq ```1 parent dc72ec8 commit f0dcf32
File tree
2 files changed
+7
-3
lines changed- clang
- lib/Sema
- test/Sema
2 files changed
+7
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11789 | 11789 | | |
11790 | 11790 | | |
11791 | 11791 | | |
11792 | | - | |
| 11792 | + | |
11793 | 11793 | | |
11794 | 11794 | | |
11795 | | - | |
| 11795 | + | |
| 11796 | + | |
11796 | 11797 | | |
11797 | 11798 | | |
11798 | 11799 | | |
| |||
11940 | 11941 | | |
11941 | 11942 | | |
11942 | 11943 | | |
11943 | | - | |
| 11944 | + | |
11944 | 11945 | | |
11945 | 11946 | | |
11946 | 11947 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
2 | 5 | | |
3 | 6 | | |
4 | 7 | | |
| |||
0 commit comments