[PATCH] D144011: [clang]Fix warning for signed conversion

Yaxun Liu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 14 07:11:42 PST 2023


yaxunl created this revision.
yaxunl added reviewers: tra, rsmith, MaskRay, rtrieu.
Herald added a project: All.
yaxunl requested review of this revision.

Currently clang emits warning with -Wconversion for the following code:

  long foo(long x) {
    return 1LL<<x;
  }

warning: implicit conversion changes signedness: 'long long' to 'long' [-Wsign-conversion]

  return 1ll << x;
  ~~~~~~ ~~~~^~~~

This does not make sense since all operands are signed.


https://reviews.llvm.org/D144011

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/Sema/sign-conversion.c


Index: clang/test/Sema/sign-conversion.c
===================================================================
--- clang/test/Sema/sign-conversion.c
+++ clang/test/Sema/sign-conversion.c
@@ -5,4 +5,8 @@
 void test(int x) {
   unsigned t0 = x; // expected-warning {{implicit conversion changes signedness}}
   unsigned t1 = (t0 == 5 ? x : 0); // expected-warning {{operand of ? changes signedness}}
+
+  // Clang has special treatment for left shift of literal '1'.
+  // Make sure there is no diagnostics.
+  long t2 = 1LL << x;
 }
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -14281,6 +14281,11 @@
       }
     }
 
+    if (SourceBT && SourceBT->isInteger() && TargetBT &&
+        TargetBT->isInteger()) {
+      return;
+    }
+
     // Fall through for non-constants to give a sign conversion warning.
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144011.497315.patch
Type: text/x-patch
Size: 949 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230214/c86ff44d/attachment.bin>


More information about the cfe-commits mailing list