Skip to content

Commit 24e2ad9

Browse files
committed
[clang][ExprConstant] Reject integral casts of addr-label-diffs...
... if the result is narrower than 32 bits. See the discussion in #136135
1 parent c15a3dd commit 24e2ad9

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18606,12 +18606,15 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
1860618606

1860718607
if (!Result.isInt()) {
1860818608
// Allow casts of address-of-label differences if they are no-ops
18609-
// or narrowing. (The narrowing case isn't actually guaranteed to
18609+
// or narrowing, if the result is at least 32 bits wide.
18610+
// (The narrowing case isn't actually guaranteed to
1861018611
// be constant-evaluatable except in some narrow cases which are hard
1861118612
// to detect here. We let it through on the assumption the user knows
1861218613
// what they are doing.)
18613-
if (Result.isAddrLabelDiff())
18614-
return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType);
18614+
if (Result.isAddrLabelDiff()) {
18615+
unsigned DestBits = Info.Ctx.getTypeSize(DestType);
18616+
return DestBits >= 32 && DestBits <= Info.Ctx.getTypeSize(SrcType);
18617+
}
1861518618
// Only allow casts of lvalues if they are lossless.
1861618619
return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1861718620
}

clang/test/CodeGenCXX/const-init.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ __int128_t PR11705 = (__int128_t)&PR11705;
7373
// CHECK: @_ZZ23UnfoldableAddrLabelDiffvE1x = internal global i128 0
7474
void UnfoldableAddrLabelDiff() { static __int128_t x = (long)&&a-(long)&&b; a:b:return;}
7575

76+
// CHECK: @_ZZ24UnfoldableAddrLabelDiff2vE1x = internal global i16 0
77+
void UnfoldableAddrLabelDiff2() { static short x = (long)&&a-(long)&&b; a:b:return;}
78+
79+
7680
// But make sure we do fold this.
7781
// CHECK: @_ZZ21FoldableAddrLabelDiffvE1x = internal global i64 sub (i64 ptrtoint (ptr blockaddress(@_Z21FoldableAddrLabelDiffv
7882
void FoldableAddrLabelDiff() { static long x = (long)&&a-(long)&&b; a:b:return;}

0 commit comments

Comments
 (0)