Skip to content

Commit 1588368

Browse files
committed
[clang][Interp] Fix casting pointers to int128
1 parent 103f1be commit 1588368

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
194194
return false;
195195

196196
PrimType T = classifyPrim(CE->getType());
197+
if (T == PT_IntAP)
198+
return this->emitCastPointerIntegralAP(Ctx.getBitWidth(CE->getType()),
199+
CE);
200+
if (T == PT_IntAPS)
201+
return this->emitCastPointerIntegralAPS(Ctx.getBitWidth(CE->getType()),
202+
CE);
197203
return this->emitCastPointerIntegral(T, CE);
198204
}
199205

clang/lib/AST/Interp/Interp.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,32 @@ bool CastPointerIntegral(InterpState &S, CodePtr OpPC) {
18331833
return true;
18341834
}
18351835

1836+
static inline bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC,
1837+
uint32_t BitWidth) {
1838+
const Pointer &Ptr = S.Stk.pop<Pointer>();
1839+
1840+
const SourceInfo &E = S.Current->getSource(OpPC);
1841+
S.CCEDiag(E, diag::note_constexpr_invalid_cast)
1842+
<< 2 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
1843+
1844+
S.Stk.push<IntegralAP<false>>(
1845+
IntegralAP<false>::from(Ptr.getIntegerRepresentation(), BitWidth));
1846+
return true;
1847+
}
1848+
1849+
static inline bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC,
1850+
uint32_t BitWidth) {
1851+
const Pointer &Ptr = S.Stk.pop<Pointer>();
1852+
1853+
const SourceInfo &E = S.Current->getSource(OpPC);
1854+
S.CCEDiag(E, diag::note_constexpr_invalid_cast)
1855+
<< 2 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
1856+
1857+
S.Stk.push<IntegralAP<true>>(
1858+
IntegralAP<true>::from(Ptr.getIntegerRepresentation(), BitWidth));
1859+
return true;
1860+
}
1861+
18361862
//===----------------------------------------------------------------------===//
18371863
// Zero, Nullptr
18381864
//===----------------------------------------------------------------------===//

clang/lib/AST/Interp/Opcodes.td

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,10 +664,19 @@ def CastFloatingIntegralAPS : Opcode {
664664
}
665665

666666
def CastPointerIntegral : Opcode {
667-
let Types = [AluTypeClass];
668-
let Args = [];
667+
let Types = [FixedSizeIntegralTypeClass];
669668
let HasGroup = 1;
670669
}
670+
def CastPointerIntegralAP : Opcode {
671+
let Types = [];
672+
let HasGroup = 0;
673+
let Args = [ArgUint32];
674+
}
675+
def CastPointerIntegralAPS : Opcode {
676+
let Types = [];
677+
let HasGroup = 0;
678+
let Args = [ArgUint32];
679+
}
671680

672681
def DecayPtr : Opcode {
673682
let Types = [PtrTypeClass, PtrTypeClass];

clang/test/AST/Interp/c.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,9 @@ int Y __attribute__((annotate(
257257
42,
258258
(struct TestStruct) { .a = 1, .b = 2 }
259259
)));
260+
261+
#ifdef __SIZEOF_INT128__
262+
const int *p = &b;
263+
const __int128 K = (__int128)(int*)0;
264+
const unsigned __int128 KU = (unsigned __int128)(int*)0;
265+
#endif

0 commit comments

Comments
 (0)