[clang] [clang][bytecode] Handle corner condition for sign negation (PR #176390)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 16 23:38:57 PST 2026
https://github.com/hax0kartik updated https://github.com/llvm/llvm-project/pull/176390
>From d293e4acd557a0361a5d02ce88d81e42283c694a Mon Sep 17 00:00:00 2001
From: hax0kartik <agarwala.kartik at gmail.com>
Date: Fri, 16 Jan 2026 19:37:54 +0530
Subject: [PATCH] [clang][bytecode] Handle corner condition for sign negation
RHS = -RHS works for most cases, however, the behaviour
when RHS is INTXX_MIN is undefined. In these particular
case(s), we should use INTXX_MAX instead.
---
clang/lib/AST/ByteCode/Interp.h | 4 +++-
clang/test/AST/ByteCode/shifts.cpp | 2 ++
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 7700b6bc5f2dd..0eae7848b4fe0 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2888,7 +2888,9 @@ inline bool DoShift(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS,
S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt();
if (!S.noteUndefinedBehavior())
return false;
- RHS = -RHS;
+
+ RHS = RHS.isMin() ? RT(APSInt::getMaxValue(RHS.bitWidth(), false)) : -RHS;
+
return DoShift<LT, RT,
Dir == ShiftDir::Left ? ShiftDir::Right : ShiftDir::Left>(
S, OpPC, LHS, RHS, Result);
diff --git a/clang/test/AST/ByteCode/shifts.cpp b/clang/test/AST/ByteCode/shifts.cpp
index f02aaf004cbd6..58118bbea7941 100644
--- a/clang/test/AST/ByteCode/shifts.cpp
+++ b/clang/test/AST/ByteCode/shifts.cpp
@@ -75,6 +75,8 @@ namespace shifts {
// ref-cxx17-warning {{shift count >= width of type}}
(void)((long)c << __CHAR_BIT__);
+ c = 1 << (int)INT_MIN; // all-warning {{shift count is negative}}
+
int i; // cxx17-warning {{uninitialized variable}} \
// ref-cxx17-warning {{uninitialized variable}}
i = 1 << (__INT_WIDTH__ - 2);
More information about the cfe-commits
mailing list