[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
Wed Jul 26 23:41:51 PDT 2023


xgupta added a comment.

In D155457#4535941 <https://reviews.llvm.org/D155457#4535941>, @aaron.ballman wrote:

> In D155457#4526168 <https://reviews.llvm.org/D155457#4526168>, @xgupta wrote:
>
>> In D155457#4523388 <https://reviews.llvm.org/D155457#4523388>, @cor3ntin wrote:
>>
>>> I'm not sure I understand the motivation for this change. Sure, people do that but they also might do the same thing for ssize_t, intmax_t, or to compare int to int32_t.
>>> I think a better heuristic would be to not emit a warning for any integral (and floating point?) type that have the same canonical types (but we probably still want one if their non-canonical type if the same)
>>
>> I am not sure but are you expecting these changes -
>>
>>   // Don't warn if the comparison involves integral or floating-point types with the same canonical types.
>>   QualType LHSCanonical = Constant->getType().getCanonicalType();
>>   QualType RHSCanonical = Other->getType().getCanonicalType();
>>   if ((LHSCanonical->isIntegralOrEnumerationType() || LHSCanonical->isFloatingType()) &&
>>       S.Context.hasSameType(LHSCanonical, RHSCanonical)) {
>>     return false;
>>   }
>>
>> This will silence a lot of warnings and a total 5 test case fails.
>
> Can you share some examples of what test cases start failing with that approach? What you have above matches what I think @cor3ntin was asking for and does seem like a pretty reasonable way to silence false positives.

Sure, updated three test cases in the patch and list the other two here -

  FAIL: Clang :: Sema/tautological-constant-compare.c (865 of 18988)
  ******************** TEST 'Clang :: Sema/tautological-constant-compare.c' FAILED ********************
  
  error: 'warning' diagnostics expected but not seen: 
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 560 (directive at /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c:568): comparison of 3-bit signed value < 4 is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 574 (directive at /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c:585): comparison of 8-bit unsigned value < 0 is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 593 (directive at /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c:595): comparison of 2-bit unsigned value > 3 is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 603: result of comparison 'int' > 2147483647 is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 608 (directive at /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c:610): comparison of 15-bit unsigned value > 32767 is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 614 (directive at /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c:616): comparison of 6-bit signed value > 31 is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 621 (directive at /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c:637): comparison of 4-bit signed value < -8 is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 623 (directive at /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c:638): comparison of 4-bit signed value > 7 is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 628 (directive at /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c:639): comparison of 5-bit signed value < -16 is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 629 (directive at /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c:640): comparison of 5-bit signed value > 15 is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 632 (directive at /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c:641): comparison of 4-bit signed value > 7 is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 633 (directive at /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c:642): comparison of 4-bit signed value < -8 is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 635 (directive at /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c:643): comparison of 5-bit signed value < -16 is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 648 (directive at /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c:659): comparison of 3-bit signed value > 3 is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 652 (directive at /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c:660): comparison of 3-bit signed value < -4 is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 656 (directive at /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c:661): comparison of 2-bit unsigned value > 3 is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c Line 657 (directive at /home/shivam/.llvm/llvm-project/clang/test/Sema/tautological-constant-compare.c:662): comparison of 2-bit unsigned value < 0 is always false
  17 errors generated.



  FAIL: Clang :: Sema/bool-compare.c (10880 of 18988)
  ******************** TEST 'Clang :: Sema/bool-compare.c' FAILED ********************
  Script:
  --
  : 'RUN: at line 1';   /home/shivam/.llvm/llvm-project/build/bin/clang -cc1 -internal-isystem /home/shivam/.llvm/llvm-project/build/lib/clang/17/include -nostdsysteminc -fsyntax-only -verify /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c -Wno-logical-not-parentheses
  --
  Exit Code: 1
  
  Command Output (stderr):
  --
  error: 'warning' diagnostics expected but not seen: 
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 8: comparison of constant 1 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 33: comparison of constant 1 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 34: comparison of constant 2 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 37: comparison of constant -1 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 39: comparison of constant 0 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 41: comparison of constant 2 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 44: comparison of constant -1 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 46: comparison of constant 0 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 48: comparison of constant 2 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 51: comparison of constant -1 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 54: comparison of constant 1 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 55: comparison of constant 2 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 58: comparison of constant -1 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 61: comparison of constant 1 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 62: comparison of constant 4 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 63: comparison of constant -1 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 66: comparison of constant 1 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 67: comparison of constant 4 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 70: comparison of constant 1 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 71: comparison of constant 4 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 73: comparison of constant -1 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 77: comparison of constant 2 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 79: comparison of constant -1 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 83: comparison of constant 2 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 85: comparison of constant -1 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 97: comparison of constant 0 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 99: comparison of constant 2 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 101: comparison of constant -1 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 104: comparison of constant 1 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 105: comparison of constant 2 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 107: comparison of constant -1 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 110: comparison of constant 1 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 111: comparison of constant 2 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 113: comparison of constant -1 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 115: comparison of constant 0 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 117: comparison of constant 2 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 119: comparison of constant -1 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 121: comparison of constant 0 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 123: comparison of constant 4 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 125: comparison of constant 0 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 127: comparison of constant 4 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 129: comparison of constant 0 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 131: comparison of constant 4 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 133: comparison of constant -1 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 137: comparison of constant 2 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 139: comparison of constant -1 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 143: comparison of constant 2 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 145: comparison of constant -1 with boolean expression is always true
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 158: comparison of constant -1 with boolean expression is always false
    File /home/shivam/.llvm/llvm-project/clang/test/Sema/bool-compare.c Line 159: comparison of constant 2 with boolean expression is always 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