Skip to content

Commit

Permalink
[clang]Fix warning for signed conversion on LP64
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yxsamliu committed Feb 21, 2023
1 parent 451799b commit 8cda128
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions clang/lib/Sema/SemaChecking.cpp
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions 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;
}

0 comments on commit 8cda128

Please sign in to comment.