Skip to content

Commit

Permalink
[clang][Interp] Consider bit width in IntegralAP::toAPSInt() (#71646)
Browse files Browse the repository at this point in the history
In `Interp.h`, when a add/sub/mul fails, we call this code and expect to
get an `APSInt` back that can handle more than the current bitwidth of
the type.
  • Loading branch information
tbaederr committed Nov 9, 2023
1 parent b7b5907 commit 8feb083
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
12 changes: 10 additions & 2 deletions clang/lib/AST/Interp/IntegralAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,16 @@ template <bool Signed> class IntegralAP final {

constexpr unsigned bitWidth() const { return V.getBitWidth(); }

APSInt toAPSInt(unsigned Bits = 0) const { return APSInt(V, !Signed); }
APValue toAPValue() const { return APValue(APSInt(V, !Signed)); }
APSInt toAPSInt(unsigned Bits = 0) const {
if (Bits == 0)
Bits = bitWidth();

if constexpr (Signed)
return APSInt(V.sext(Bits), !Signed);
else
return APSInt(V.zext(Bits), !Signed);
}
APValue toAPValue() const { return APValue(toAPSInt()); }

bool isZero() const { return V.isZero(); }
bool isPositive() const { return V.isNonNegative(); }
Expand Down
9 changes: 7 additions & 2 deletions clang/test/AST/Interp/intap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ namespace i128 {

static const __int128_t INT128_MAX = UINT128_MAX >> (__int128_t)1;
static_assert(INT128_MAX != 0, "");
static_assert(INT128_MAX == 0, ""); // expected-error {{failed}} \
// expected-note {{evaluates to '170141183460469231731687303715884105727 == 0'}} \
// ref-error {{failed}} \
// ref-note {{evaluates to '170141183460469231731687303715884105727 == 0'}}

static const __int128_t INT128_MIN = -INT128_MAX - 1;
constexpr __int128 A = INT128_MAX + 1; // expected-error {{must be initialized by a constant expression}} \
// expected-note {{outside the range}} \
// expected-note {{value 170141183460469231731687303715884105728 is outside the range}} \
// ref-error {{must be initialized by a constant expression}} \
// ref-note {{outside the range}}
// ref-note {{value 170141183460469231731687303715884105728 is outside the range}}
constexpr int128_t Two = (int128_t)1 << 1ul;
static_assert(Two == 2, "");
static_assert(Two, "");
Expand Down

0 comments on commit 8feb083

Please sign in to comment.