[PATCH] D155457: [clang] Skip tautological comparison if the comparison involves the 'size_t' type

Shivam Gupta via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 18 05:45:27 PDT 2023


xgupta added a comment.

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.

  // RUN: %clang_cc1 %s -fsyntax-only -Wtautological-type-limit-compare -verify
  
  // expected-no-diagnostics
  
  typedef unsigned long long uint64_t;
  namespace std {
  using size_t = decltype(sizeof(0));
  } // namespace std
  
  bool func(uint64_t Size) {
    if (sizeof(std::size_t) < sizeof(uint64_t) &&
       Size > (uint64_t)(__SIZE_MAX__))
      return false;
    return true;
  }


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