Skip to content

Commit

Permalink
[clang][Interp] Check Neg ops for errors
Browse files Browse the repository at this point in the history
This should fail when negating __INT_MIN__.

Differential Revision: https://reviews.llvm.org/D148987
  • Loading branch information
tbaederr committed Apr 27, 2023
1 parent 8babf8f commit 6cf14a7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
3 changes: 3 additions & 0 deletions clang/lib/AST/Interp/Integral.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ template <unsigned Bits, bool Signed> class Integral final {
}

static bool neg(Integral A, Integral *R) {
if (Signed && A.isMin())
return true;

*R = -A;
return false;
}
Expand Down
26 changes: 23 additions & 3 deletions clang/lib/AST/Interp/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,32 @@ bool Inv(InterpState &S, CodePtr OpPC) {

template <PrimType Name, class T = typename PrimConv<Name>::T>
bool Neg(InterpState &S, CodePtr OpPC) {
const T &Val = S.Stk.pop<T>();
const T &Value = S.Stk.pop<T>();
T Result;
T::neg(Val, &Result);

if (!T::neg(Value, &Result)) {
S.Stk.push<T>(Result);
return true;
}

assert(isIntegralType(Name) &&
"don't expect other types to fail at constexpr negation");
S.Stk.push<T>(Result);
return true;

APSInt NegatedValue = -Value.toAPSInt(Value.bitWidth() + 1);
const Expr *E = S.Current->getExpr(OpPC);
QualType Type = E->getType();

if (S.checkingForUndefinedBehavior()) {
SmallString<32> Trunc;
NegatedValue.trunc(Result.bitWidth()).toString(Trunc, 10);
auto Loc = E->getExprLoc();
S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
return true;
}

S.CCEDiag(E, diag::note_constexpr_overflow) << NegatedValue << Type;
return S.noteUndefinedBehavior();
}

enum class PushVal : bool {
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/AST/Interp/PrimType.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ enum PrimType : unsigned {
PT_FnPtr,
};

constexpr bool isIntegralType(PrimType T) { return T <= PT_Uint64; }

/// Mapping from primitive types to their representation.
template <PrimType T> struct PrimConv;
template <> struct PrimConv<PT_Sint8> { using T = Integral<8, true>; };
Expand Down
6 changes: 6 additions & 0 deletions clang/test/AST/Interp/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ static_assert(~255 == -256, "");
static_assert(~INT_MIN == INT_MAX, "");
static_assert(~INT_MAX == INT_MIN, "");

static_assert(-(1 << 31), ""); // expected-error {{not an integral constant expression}} \
// expected-note {{outside the range of representable values}} \
// ref-error {{not an integral constant expression}} \
// ref-note {{outside the range of representable values}} \

enum E {};
constexpr E e = static_cast<E>(0);
static_assert(~e == -1, "");
Expand Down

0 comments on commit 6cf14a7

Please sign in to comment.