[clang] [clang][bytecode] Fix shifting by negative IntAP values (PR #202505)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 8 21:49:10 PDT 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/202505
The negation of a negative value didn't necessarily result in a positive value. Fix that by giving it one more bit of precision.
>From b3767085f160519c34acdd867c9e7cc6000ab225 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 9 Jun 2026 06:45:06 +0200
Subject: [PATCH] [clang][bytecode] Fix shifting by negative IntAP values
The negation of a negative value didn't necessarily result in a positive
value. Fix that by giving it one more bit of precision.
---
clang/lib/AST/ByteCode/Interp.h | 2 +-
clang/test/AST/ByteCode/intap.cpp | 10 ++++++++--
2 files changed, 9 insertions(+), 3 deletions(-)
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}}
More information about the cfe-commits
mailing list