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.
   }
 


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.
   }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to