This is an archive of the discontinued LLVM Phabricator instance.

[clang][CGExprConstant] handle implicit widening/narrowing Int-to-Int casts
ClosedPublic

Authored by nickdesaulniers on Jul 27 2023, 10:58 AM.

Details

Summary

Consider the following statements:

long x = 1;
short y = 1;

With the following AST:

|-VarDecl 0x55d289973730 <x.c:1:1, col:10> col:6 x 'long' cinit
| `-ImplicitCastExpr 0x55d289973800 <col:10> 'long' <IntegralCast>
|   `-IntegerLiteral 0x55d2899737e0 <col:10> 'int' 1
`-VarDecl 0x55d289973830 <line:2:1, col:11> col:7 y 'short' cinit
  `-ImplicitCastExpr 0x55d2899738b8 <col:11> 'short' <IntegralCast>
    `-IntegerLiteral 0x55d289973898 <col:11> 'int' 1

Sign or Zero extend or truncate based on the source signedness and
destination width.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2023, 10:58 AM
nickdesaulniers requested review of this revision.Jul 27 2023, 10:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2023, 10:58 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
shafik added a comment.EditedJul 27 2023, 4:17 PM

LGTM but I want @efriedma to approve

efriedma added inline comments.Aug 2 2023, 3:53 PM
clang/lib/CodeGen/CGExprConstant.cpp
1152

sextOrTrunc() is, in general, wrong; if FromType is unsigned, you need zextOrTrunc. (This works transparently in HandleIntToIntCast because it's using APSInt, which tracks the sign bit of the operand.)

nickdesaulniers marked an inline comment as done.
  • add test case that would have caught SExt bug
  • fix SExt bug
nickdesaulniers edited the summary of this revision. (Show Details)
  • update description
This revision is now accepted and ready to land.Aug 4 2023, 10:05 AM