Skip to content

Commit 3efa479

Browse files
authored
[clang][Interp] Support AddOffset with 128bit offsets (#68679)
We do a similar thing a few lines above for `Index`: ```c++ // Get a version of the index comparable to the type. T Index = T::from(Ptr.getIndex(), Offset.bitWidth()); ```
1 parent 8d59fc5 commit 3efa479

File tree

5 files changed

+19
-2
lines changed

5 files changed

+19
-2
lines changed

clang/lib/AST/Interp/Boolean.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Boolean final {
4242
bool operator>(unsigned RHS) const { return static_cast<unsigned>(V) > RHS; }
4343

4444
Boolean operator-() const { return Boolean(V); }
45+
Boolean operator-(const Boolean &Other) const { return Boolean(V - Other.V); }
4546
Boolean operator~() const { return Boolean(true); }
4647

4748
explicit operator int8_t() const { return V; }

clang/lib/AST/Interp/Integral.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ template <unsigned Bits, bool Signed> class Integral final {
8888
}
8989

9090
Integral operator-() const { return Integral(-V); }
91+
Integral operator-(const Integral &Other) const {
92+
return Integral(V - Other.V);
93+
}
9194
Integral operator~() const { return Integral(~V); }
9295

9396
template <unsigned DstBits, bool DstSign>

clang/lib/AST/Interp/IntegralAP.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ template <bool Signed> class IntegralAP final {
5959
IntegralAP() : V(APSInt::getMaxValue(1024, Signed)) {}
6060

6161
IntegralAP operator-() const { return IntegralAP(-V); }
62+
IntegralAP operator-(const IntegralAP &Other) const {
63+
return IntegralAP(V - Other.V);
64+
}
6265
bool operator>(IntegralAP RHS) const { return V > RHS.V; }
6366
bool operator>=(IntegralAP RHS) const { return V >= RHS.V; }
6467
bool operator<(IntegralAP RHS) const { return V < RHS.V; }

clang/lib/AST/Interp/Interp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,7 @@ bool OffsetHelper(InterpState &S, CodePtr OpPC, const T &Offset,
14211421
// Get a version of the index comparable to the type.
14221422
T Index = T::from(Ptr.getIndex(), Offset.bitWidth());
14231423
// Compute the largest index into the array.
1424-
unsigned MaxIndex = Ptr.getNumElems();
1424+
T MaxIndex = T::from(Ptr.getNumElems(), Offset.bitWidth());
14251425

14261426
// Helper to report an invalid offset, computed as APSInt.
14271427
auto InvalidOffset = [&]() {
@@ -1437,7 +1437,7 @@ bool OffsetHelper(InterpState &S, CodePtr OpPC, const T &Offset,
14371437
return false;
14381438
};
14391439

1440-
unsigned MaxOffset = MaxIndex - Ptr.getIndex();
1440+
T MaxOffset = T::from(MaxIndex - Index, Offset.bitWidth());
14411441
if constexpr (Op == ArithOp::Add) {
14421442
// If the new offset would be negative, bail out.
14431443
if (Offset.isNegative() && (Offset.isMin() || -Offset > Index))

clang/test/AST/Interp/intap.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,14 @@ namespace i128 {
9090
// expected-error {{must be initialized by a constant expression}} \
9191
// expected-note {{is outside the range of representable values of type}}
9292
}
93+
94+
namespace AddSubOffset {
95+
constexpr __int128 A = 1;
96+
constexpr int arr[] = {1,2,3};
97+
constexpr const int *P = arr + A;
98+
static_assert(*P == 2, "");
99+
constexpr const int *P2 = P - A;
100+
static_assert(*P2 == 1,"");
101+
}
102+
93103
#endif

0 commit comments

Comments
 (0)