[clang] 8cda128 - [clang]Fix warning for signed conversion on LP64

Yaxun Liu via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 21 09:44:05 PST 2023


Author: Yaxun (Sam) Liu
Date: 2023-02-21T12:43:42-05:00
New Revision: 8cda128c1eff8556505ff27117a68ad8cecaa228

URL: https://github.com/llvm/llvm-project/commit/8cda128c1eff8556505ff27117a68ad8cecaa228
DIFF: https://github.com/llvm/llvm-project/commit/8cda128c1eff8556505ff27117a68ad8cecaa228.diff

LOG: [clang]Fix warning for signed conversion on LP64

Currently clang emits warning with -Wconversion for the following code
on LP64 system e.g. x86_64-unknown-linux-gnu:

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.

This patch fixes that to match -m32 and GCC behaviour.

Reviewed by: Fangrui Song

Differential Revision: https://reviews.llvm.org/D144011

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 9d5490a99310f..eded6061c77eb 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -14316,6 +14316,12 @@ static void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
     if (S.SourceMgr.isInSystemMacro(CC))
       return;
 
+    if (SourceBT && SourceBT->isInteger() && TargetBT &&
+        TargetBT->isInteger() &&
+        Source->isSignedIntegerType() == Target->isSignedIntegerType()) {
+      return;
+    }
+
     unsigned DiagID = diag::warn_impcast_integer_sign;
 
     // Traditionally, gcc has warned about this under -Wsign-compare.

diff  --git a/clang/test/Sema/sign-conversion.c b/clang/test/Sema/sign-conversion.c
index 4b1ee7542158d..e96ab18a46ed0 100644
--- a/clang/test/Sema/sign-conversion.c
+++ b/clang/test/Sema/sign-conversion.c
@@ -1,8 +1,14 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -Wsign-conversion %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fsyntax-only -verify -Wsign-conversion %s
+// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -fsyntax-only -verify -Wsign-conversion %s
 
 // PR9345: make a subgroup of -Wconversion for signedness changes
 
 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;
 }


        


More information about the cfe-commits mailing list