[PATCH] D155457: [clang] Skip tautological comparison if the comparison involves the 'size_t' type
Aaron Ballman via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 18 09:56:24 PDT 2023
aaron.ballman added a comment.
In D155457#4510202 <https://reviews.llvm.org/D155457#4510202>, @xgupta wrote:
> In D155457#4507124 <https://reviews.llvm.org/D155457#4507124>, @aaron.ballman wrote:
>
>> In D155457#4507107 <https://reviews.llvm.org/D155457#4507107>, @xgupta wrote:
>>
>>> In D155457#4506405 <https://reviews.llvm.org/D155457#4506405>, @xbolva00 wrote:
>>>
>>>> You should add a testcase which uses “expected no diagnostics”.
>>>
>>> There is some issue writing test
>>>
>>> $ cat type-limit-compare.cpp
>>> // RUN: %clang_cc1 -fsyntax-only -Wtautological-type-limit-compare -verify %s
>>> // expected-no-diagnostics
>>>
>>> #include <cstddef>
>>> #include <cstdint>
>>> #include <limits>
>>>
>>> bool foo(uint64_t Size) {
>>> if (sizeof(std::size_t) < sizeof(uint64_t) &&
>>> Size > static_cast<uint64_t>(std::numeric_limits<std::size_t>::max())) // no-warning
>>> return false;
>>> return true;
>>> }
>>>
>>> failed with
>>>
>>> $ llvm-project/clang/test/Sema/type-limit-compare.cpp:4:10: fatal error: 'cstddef' file not found
>>> 4 | #include <cstddef>
>>> | ^~~~~~~~~
>>
>> We typically do not include any system headers (STL or otherwise) as part of the compiler tests; that would test whatever is found on the test system instead of a consistent test. Instead, I'd recommend doing:
>>
>> namespace std {
>> using size_t = decltype(sizeof(0));
>> }
>>
>> Similarly, you can replace `uint64_t` with `unsigned long long` and the `numeric_limits` call with `__SIZE_MAX__`
>
> I see, Thanks, but there is another thing, writing this way compiler emits a warning as the check to exclude the warning is based on `size_t` so the test case is not passed.
Whether `size_t` comes from the system header or whether it's manually deduced from `decltype(sizeof(0))` should make no difference as far as the frontend is concerned; they should resolve to the same type. Can you explain the test failure you're seeing in a bit more detail?
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D155457/new/
https://reviews.llvm.org/D155457
More information about the cfe-commits
mailing list