This is an archive of the discontinued LLVM Phabricator instance.

[clang]Fix warning for signed conversion on LP64
ClosedPublic

Authored by yaxunl on Feb 14 2023, 7:11 AM.

Details

Summary

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.

Diff Detail

Event Timeline

yaxunl created this revision.Feb 14 2023, 7:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 14 2023, 7:11 AM
yaxunl requested review of this revision.Feb 14 2023, 7:11 AM
shafik added a subscriber: shafik.Feb 14 2023, 9:51 AM

If I look at the clang docs for Wconversion I see it includes -Wshorten-64-to-32 which I believe this is a case of. I think maybe the warning needs a better warning for this case?

If I look at the clang docs for Wconversion I see it includes -Wshorten-64-to-32 which I believe this is a case of. I think maybe the warning needs a better warning for this case?

This is done for x86_64 on linux, where long long and long are both 64bit. https://godbolt.org/z/hd3qWW5jj

MaskRay added a comment.EditedFeb 14 2023, 11:07 AM

I think it makes sense for -Wsign-conversion to not warn for this LP64 case, like we don't emit a warning for -m32. I do not know whether we need another diagnostic like -Wshorten-64-to-32 for this case, but am inclined to no.

I wonder whether the newly added condition can be merged with the following condition:

if ((!isa<EnumType>(Target) || !isa<EnumType>(Source)) &&
    ((TargetRange.NonNegative && !LikelySourceRange.NonNegative) ||
     (!TargetRange.NonNegative && LikelySourceRange.NonNegative &&
      LikelySourceRange.Width == TargetRange.Width))) {
  if (S.SourceMgr.isInSystemMacro(CC))

I think it makes sense for -Wsign-conversion to not warn for this LP64 case, like we don't emit a warning for -m32. I do not know whether we need another diagnostic like -Wshorten-64-to-32 for this case, but am inclined to no.

I wonder whether the newly added condition can be merged with the following condition:

if ((!isa<EnumType>(Target) || !isa<EnumType>(Source)) &&
    ((TargetRange.NonNegative && !LikelySourceRange.NonNegative) ||
     (!TargetRange.NonNegative && LikelySourceRange.NonNegative &&
      LikelySourceRange.Width == TargetRange.Width))) {
  if (S.SourceMgr.isInSystemMacro(CC))

Yes. Will do.

yaxunl updated this revision to Diff 497703.Feb 15 2023, 9:19 AM

revised by Fanrui's comments

MaskRay accepted this revision.Feb 17 2023, 2:11 PM

Thanks!

clang/lib/Sema/SemaChecking.cpp
14296

Consider adding an example when this condition triggers to help understanding, e.g. long t2 = 1LL << x; on LP64 systems.

This revision is now accepted and ready to land.Feb 17 2023, 2:11 PM

The description needs to adjusted to say that this confusing warning is for LP64 systems.
It makes sense to match -m32 and GCC. If someone thinks a warning is useful, that can be a separate change.

yaxunl retitled this revision from [clang]Fix warning for signed conversion to [clang]Fix warning for signed conversion on LP64.Feb 17 2023, 7:51 PM
yaxunl edited the summary of this revision. (Show Details)
yaxunl marked an inline comment as done.Feb 17 2023, 7:52 PM
yaxunl added inline comments.
clang/lib/Sema/SemaChecking.cpp
14296

will do when committing

This revision was landed with ongoing or failed builds.Feb 21 2023, 9:44 AM
This revision was automatically updated to reflect the committed changes.
yaxunl marked an inline comment as done.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 21 2023, 9:44 AM