[clang] [clang][bytecode] Fix shifting by negative IntAP values (PR #202505)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 8 21:49:43 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
The negation of a negative value didn't necessarily result in a positive value. Fix that by giving it one more bit of precision.
---
Full diff: https://github.com/llvm/llvm-project/pull/202505.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Interp.h (+1-1)
- (modified) clang/test/AST/ByteCode/intap.cpp (+8-2)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index d2ca122d0e805..38202582b653e 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -3257,7 +3257,7 @@ inline bool DoShiftAP(InterpState &S, CodePtr OpPC, const APSInt &LHS,
return false;
return DoShiftAP<LT, RT,
Dir == ShiftDir::Left ? ShiftDir::Right : ShiftDir::Left>(
- S, OpPC, LHS, -RHS, Result);
+ S, OpPC, LHS, -(RHS.extend(RHS.getBitWidth() + 1)), Result);
}
if (!CheckShift<Dir>(S, OpPC, static_cast<LT>(LHS), static_cast<RT>(RHS),
diff --git a/clang/test/AST/ByteCode/intap.cpp b/clang/test/AST/ByteCode/intap.cpp
index 5c3e9f979e129..c9904cca8d76c 100644
--- a/clang/test/AST/ByteCode/intap.cpp
+++ b/clang/test/AST/ByteCode/intap.cpp
@@ -1,7 +1,7 @@
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -fms-extensions -std=c++11 -verify=expected,both %s
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -fms-extensions -std=c++20 -verify=expected,both %s
-// RUN: %clang_cc1 -std=c++11 -fms-extensions -verify=ref,both %s
-// RUN: %clang_cc1 -std=c++20 -fms-extensions -verify=ref,both %s
+// RUN: %clang_cc1 -fms-extensions -std=c++11 -verify=ref,both %s
+// RUN: %clang_cc1 -fms-extensions -std=c++20 -verify=ref,both %s
using MaxBitInt = _BitInt(128);
@@ -298,6 +298,12 @@ namespace IncDec {
#endif
}
+namespace NegShift {
+ constexpr __int128_t a = ((__int128_t)1 << 127);
+ static_assert((2 >> a) == 1, ""); // both-error {{not an integral constant expression}} \
+ // both-note {{negative shift count -170141183460469231731687303715884105728}}
+}
+
#if __cplusplus >= 201402L
const __int128_t a = ( (__int128_t)1 << 64 );
const _BitInt(72) b = ( 1 << 72 ); // both-warning {{shift count >= width of type}}
``````````
</details>
https://github.com/llvm/llvm-project/pull/202505
More information about the cfe-commits
mailing list