[PATCH] D126960: [clang][sema] Unary not boolean to int conversion

Ashley Roll via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jun 4 00:08:11 PDT 2022


AshleyRoll updated this revision to Diff 434250.
AshleyRoll added a comment.

Rebased to remove clangd test failure


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126960/new/

https://reviews.llvm.org/D126960

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/Sema/compare.c


Index: clang/test/Sema/compare.c
===================================================================
--- clang/test/Sema/compare.c
+++ clang/test/Sema/compare.c
@@ -419,3 +419,25 @@
   if (x == y) x = y; // no warning
   if (y == x) y = x; // no warning
 }
+
+int warn_on_different_sign_after_unary_operator(unsigned a, int b) {
+  return
+      // unary not promotes boolean to int
+      (a > ~(!b)) // expected-warning {{comparison of integers of different signs: 'unsigned int' and 'int'}}
+      &&
+      (a > -(b)) // expected-warning {{comparison of integers of different signs: 'unsigned int' and 'int'}}
+      &&
+      (a > ++b) // expected-warning {{comparison of integers of different signs: 'unsigned int' and 'int'}}
+      &&
+      (a > --b) // expected-warning {{comparison of integers of different signs: 'unsigned int' and 'int'}}
+      &&
+      // unary not promotes boolean to int
+      (b > ~(!a)) // no warning
+      &&
+      (b > -(a)) // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}}
+      &&
+      (b > ++a) // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}}
+      &&
+      (b > --a) // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}}
+      ;
+}
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -12326,6 +12326,13 @@
     case UO_AddrOf: // should be impossible
       return IntRange::forValueOfType(C, GetExprType(E));
 
+    case UO_Not:
+      // unary not promotes boolean to integer
+      if (UO->getSubExpr()->isKnownToHaveBooleanValue())
+        return IntRange(MaxWidth, false);
+
+      LLVM_FALLTHROUGH;
+
     default:
       return GetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext,
                           Approximate);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126960.434250.patch
Type: text/x-patch
Size: 1947 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220604/8674a19c/attachment.bin>


More information about the cfe-commits mailing list