Author: Kartik
Date: 2026-01-20T06:24:40+01:00
New Revision: 7dc2cd4c6840f0c3eeca4d822709a7605836d000

URL: 
https://github.com/llvm/llvm-project/commit/7dc2cd4c6840f0c3eeca4d822709a7605836d000
DIFF: 
https://github.com/llvm/llvm-project/commit/7dc2cd4c6840f0c3eeca4d822709a7605836d000.diff

LOG: [clang][bytecode] Handle corner condition for sign negation (#176390)

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.

Fixes #176271.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Interp.h
    clang/test/AST/ByteCode/shifts.cpp

Removed: 
    


################################################################################
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);


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to