[clang] 00e967f - [clang][Interp] Fix left-/right-shifting more than sizeof(unsigned)
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 25 22:07:41 PST 2023
Author: Timm Bäder
Date: 2023-01-26T07:07:17+01:00
New Revision: 00e967f6c2d626d1913f5af5763beab7946978ce
URL: https://github.com/llvm/llvm-project/commit/00e967f6c2d626d1913f5af5763beab7946978ce
DIFF: https://github.com/llvm/llvm-project/commit/00e967f6c2d626d1913f5af5763beab7946978ce.diff
LOG: [clang][Interp] Fix left-/right-shifting more than sizeof(unsigned)
We were just casting to `unsigned` before, so that caused problems when
shifting more bits than `unsigned` has.
Differential Revision: https://reviews.llvm.org/D140845
Added:
Modified:
clang/lib/AST/Interp/Integral.h
clang/lib/AST/Interp/Interp.h
clang/test/AST/Interp/shifts.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/Interp/Integral.h b/clang/lib/AST/Interp/Integral.h
index d5368167d967..0eb0990a0334 100644
--- a/clang/lib/AST/Interp/Integral.h
+++ b/clang/lib/AST/Interp/Integral.h
@@ -234,6 +234,18 @@ template <unsigned Bits, bool Signed> class Integral final {
return false;
}
+ template <unsigned RHSBits, bool RHSSign>
+ static void shiftLeft(const Integral A, const Integral<RHSBits, RHSSign> B,
+ unsigned OpBits, Integral *R) {
+ *R = Integral::from(A.V << B.V, OpBits);
+ }
+
+ template <unsigned RHSBits, bool RHSSign>
+ static void shiftRight(const Integral A, const Integral<RHSBits, RHSSign> B,
+ unsigned OpBits, Integral *R) {
+ *R = Integral::from(A.V >> B.V, OpBits);
+ }
+
private:
template <typename T> static bool CheckAddUB(T A, T B, T &R) {
if constexpr (std::is_signed_v<T>) {
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 466df04ac08d..b36b513c067f 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -1334,8 +1334,9 @@ inline bool Shr(InterpState &S, CodePtr OpPC) {
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()));
+ Integral<LT::bitWidth(), false> R;
+ Integral<LT::bitWidth(), false>::shiftRight(LHS.toUnsigned(), RHS, Bits, &R);
+ S.Stk.push<LT>(R);
return true;
}
@@ -1350,9 +1351,9 @@ inline bool Shl(InterpState &S, CodePtr OpPC) {
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()));
-
+ Integral<LT::bitWidth(), false> R;
+ Integral<LT::bitWidth(), false>::shiftLeft(LHS.toUnsigned(), RHS, Bits, &R);
+ S.Stk.push<LT>(R);
return true;
}
diff --git a/clang/test/AST/Interp/shifts.cpp b/clang/test/AST/Interp/shifts.cpp
index 8fa78beb9302..a9193258e6f5 100644
--- a/clang/test/AST/Interp/shifts.cpp
+++ b/clang/test/AST/Interp/shifts.cpp
@@ -143,4 +143,9 @@ namespace shifts {
constexpr char c2 = 1;
constexpr int i3 = c2 << (__CHAR_BIT__ + 1); // Not ill-formed
+
+ constexpr signed long int L = 1;
+ constexpr signed int R = 62;
+ constexpr decltype(L) M = L << R;
+ constexpr decltype(L) M2 = L >> R;
};
More information about the cfe-commits
mailing list