diff --git a/clang/lib/AST/Interp/Interp.cpp b/clang/lib/AST/Interp/Interp.cpp index 6a600b306bad4..76ade4401e085 100644 --- a/clang/lib/AST/Interp/Interp.cpp +++ b/clang/lib/AST/Interp/Interp.cpp @@ -401,6 +401,27 @@ bool CheckPure(InterpState &S, CodePtr OpPC, const CXXMethodDecl *MD) { return false; } +template +bool CheckShift(InterpState &S, CodePtr OpPC, const RT &RHS, unsigned Bits) { + if (RHS.isNegative()) { + const SourceInfo &Loc = S.Current->getSource(OpPC); + S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt(); + return false; + } + + // C++11 [expr.shift]p1: Shift width must be less than the bit width of + // the shifted type. + if (Bits > 1 && RHS >= RT::from(Bits, RHS.bitWidth())) { + const Expr *E = S.Current->getExpr(OpPC); + const APSInt Val = RHS.toAPSInt(); + QualType Ty = E->getType(); + S.CCEDiag(E, diag::note_constexpr_large_shift) << Val << Ty << Bits; + return false; + } + + return true; +} + static void DiagnoseUninitializedSubobject(InterpState &S, const SourceInfo &SI, QualType SubObjType, SourceLocation SubObjLoc) { diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h index 65a49f21c5dc2..903c68f4511c6 100644 --- a/clang/lib/AST/Interp/Interp.h +++ b/clang/lib/AST/Interp/Interp.h @@ -94,6 +94,10 @@ bool CheckPure(InterpState &S, CodePtr OpPC, const CXXMethodDecl *MD); /// Checks that all fields are initialized after a constructor call. bool CheckCtorCall(InterpState &S, CodePtr OpPC, const Pointer &This); +/// Checks if the shift operation is legal. +template +bool CheckShift(InterpState &S, CodePtr OpPC, const RT &RHS, unsigned Bits); + /// Checks if Div/Rem operation on LHS and RHS is valid. template bool CheckDivRem(InterpState &S, CodePtr OpPC, const T &LHS, const T &RHS) { @@ -1180,21 +1184,8 @@ inline bool Shr(InterpState &S, CodePtr OpPC) { const auto &LHS = S.Stk.pop(); const unsigned Bits = LHS.bitWidth(); - if (RHS.isNegative()) { - const SourceInfo &Loc = S.Current->getSource(OpPC); - S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt(); + if (!CheckShift(S, OpPC, RHS, Bits)) return false; - } - - // C++11 [expr.shift]p1: Shift width must be less than the bit width of - // the shifted type. - if (Bits > 1 && RHS >= RT::from(Bits, RHS.bitWidth())) { - const Expr *E = S.Current->getExpr(OpPC); - const APSInt Val = RHS.toAPSInt(); - QualType Ty = E->getType(); - S.CCEDiag(E, diag::note_constexpr_large_shift) << Val << Ty << Bits; - return false; - } unsigned URHS = static_cast(RHS); S.Stk.push(LT::from(static_cast(LHS) >> URHS, LHS.bitWidth())); @@ -1209,21 +1200,8 @@ inline bool Shl(InterpState &S, CodePtr OpPC) { const auto &LHS = S.Stk.pop(); const unsigned Bits = LHS.bitWidth(); - if (RHS.isNegative()) { - const SourceInfo &Loc = S.Current->getSource(OpPC); - S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt(); - return false; - } - - // C++11 [expr.shift]p1: Shift width must be less than the bit width of - // the shifted type. - if (Bits > 1 && RHS >= RT::from(Bits, RHS.bitWidth())) { - const Expr *E = S.Current->getExpr(OpPC); - const APSInt Val = RHS.toAPSInt(); - QualType Ty = E->getType(); - S.CCEDiag(E, diag::note_constexpr_large_shift) << Val << Ty << Bits; + if (!CheckShift(S, OpPC, RHS, Bits)) return false; - } unsigned URHS = static_cast(RHS); S.Stk.push(LT::from(static_cast(LHS) << URHS, LHS.bitWidth()));