[clang] fddf641 - [clang][Interp][NFC] Remove shift error checking code duplication
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 19 03:23:19 PST 2023
Author: Timm Bäder
Date: 2023-01-19T12:18:26+01:00
New Revision: fddf6418e8492a544c9bfdb42a4dbc949d9dc2ee
URL: https://github.com/llvm/llvm-project/commit/fddf6418e8492a544c9bfdb42a4dbc949d9dc2ee
DIFF: https://github.com/llvm/llvm-project/commit/fddf6418e8492a544c9bfdb42a4dbc949d9dc2ee.diff
LOG: [clang][Interp][NFC] Remove shift error checking code duplication
Added:
Modified:
clang/lib/AST/Interp/Interp.cpp
clang/lib/AST/Interp/Interp.h
Removed:
################################################################################
diff --git a/clang/lib/AST/Interp/Interp.cpp b/clang/lib/AST/Interp/Interp.cpp
index 6a600b306bad..76ade4401e08 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 <typename RT>
+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 65a49f21c5dc..903c68f4511c 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 <typename RT>
+bool CheckShift(InterpState &S, CodePtr OpPC, const RT &RHS, unsigned Bits);
+
/// Checks if Div/Rem operation on LHS and RHS is valid.
template <typename T>
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<LT>();
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<RT>(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<unsigned>(RHS);
S.Stk.push<LT>(LT::from(static_cast<unsigned>(LHS) >> URHS, LHS.bitWidth()));
@@ -1209,21 +1200,8 @@ inline bool Shl(InterpState &S, CodePtr OpPC) {
const auto &LHS = S.Stk.pop<LT>();
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<RT>(S, OpPC, RHS, Bits))
return false;
- }
unsigned URHS = static_cast<unsigned>(RHS);
S.Stk.push<LT>(LT::from(static_cast<unsigned>(LHS) << URHS, LHS.bitWidth()));
More information about the cfe-commits
mailing list