Author: Shengxin Pei
Date: 2026-08-23T06:40:58+08:00
New Revision: 3cd29d939bad87b6ae4cc806e8d5a91855abbccd

URL: 
https://github.com/llvm/llvm-project/commit/3cd29d939bad87b6ae4cc806e8d5a91855abbccd
DIFF: 
https://github.com/llvm/llvm-project/commit/3cd29d939bad87b6ae4cc806e8d5a91855abbccd.diff

LOG: [Clang] Fixed an assertion caused by Microsoft integer literals exceeding 
the maximum value (#212743)

Rewrites the truncation logic for Microsoft integer literals.

Fix #212504

---------

Co-authored-by: A. Jiang <[email protected]>

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.md
    clang/lib/Sema/SemaExpr.cpp
    clang/test/SemaCXX/ms_integer_suffix.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index df8479a924771..fcd58e38261bb 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -430,6 +430,7 @@ features cannot lower the translation-unit ABI level;
 - Fixed a bug where `__func__`, `__PRETTY_FUNCTION__` and `__FUNCTION__` were 
not resolving to the proper function when inside a lambda return type 
(#GH211811)
 - Fixed USR generation for declarations whose signature mentions a class-type
   non-type template parameter. (#GH212351)
+- Fixed an assertion caused by Microsoft integer literals exceeding the 
maximum value. (#GH212504)
 - Fixed a crash when checking scalar type with excess braces. (#GH69213), 
(#GH137845), (#GH198767), (#GH207566), (#GH106180)
 - Fixed an assertion crash when instantiating a nested requirement with an 
invalid constraint. (#GH213575)
 - Clang now defines the GCC-compatible predefined macro `__SIG_ATOMIC_TYPE__`. 
(#GH213895)

diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index da76bbf3c35f0..99efe330d6749 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -4091,7 +4091,7 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, 
Scope *UDLScope) {
           !Context.getTargetInfo().hasInt128Type())
         PP.Diag(Tok.getLocation(), diag::err_integer_literal_too_large)
             << Literal.isUnsigned;
-      BitsNeeded = Literal.MicrosoftInteger;
+      BitsNeeded = std::max<unsigned>(BitsNeeded, Literal.MicrosoftInteger);
     }
 
     llvm::APInt ResultVal(BitsNeeded, 0);
@@ -4133,6 +4133,9 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, 
Scope *UDLScope) {
           Ty = Context.getIntTypeForBitwidth(Width,
                                              /*Signed=*/!Literal.isUnsigned);
         }
+        // To maintain consistency with MSVC, we chose to truncate directly
+        // without issuing any warnings.
+        ResultVal = ResultVal.zextOrTrunc(Width);
       }
 
       // Bit-precise integer literals are automagically-sized based on the

diff  --git a/clang/test/SemaCXX/ms_integer_suffix.cpp 
b/clang/test/SemaCXX/ms_integer_suffix.cpp
index aa2f13099d3b8..d36a560246a04 100644
--- a/clang/test/SemaCXX/ms_integer_suffix.cpp
+++ b/clang/test/SemaCXX/ms_integer_suffix.cpp
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fms-extensions -verify %s
-// expected-no-diagnostics
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fms-extensions 
-verify=signed,expected %s
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fms-extensions -fno-signed-char 
-verify=unsigned,expected %s
 
 #ifdef __SIZEOF_INT8__
 static_assert(sizeof(0i8) == __SIZEOF_INT8__, "");
@@ -18,3 +18,24 @@ static_assert(sizeof(0i32) == __SIZEOF_INT32__, "");
 #ifdef __SIZEOF_INT64__
 static_assert(sizeof(0i64) == __SIZEOF_INT64__, "");
 #endif
+
+namespace gh212504 {
+  static_assert(1234i8 == -46, ""); // unsigned-error {{static assertion 
failed due to requirement '210i8 == -46':}}
+  static_assert(1234i8 == 210, ""); // signed-error {{static assertion failed 
due to requirement '-46i8 == 210':}}
+  static_assert(1234ui8 == 210, "");
+  static_assert(123456i16 == -7616, "");
+  static_assert(123456ui16 == 57920, "");
+  static_assert(12345678901i32 == -539222987, "");
+  static_assert(12345678901ui32 == 3755744309, "");
+  static_assert(18446744073709551615i8, "");
+  static_assert(18446744073709551615ui32, "");
+
+  static_assert(18446744073709551616i8 == 0, ""); // expected-error {{integer 
literal is too large to be represented in any integer type}}
+  static_assert(18446744073709551616i16 == 0, ""); // expected-error {{integer 
literal is too large to be represented in any integer type}}
+  static_assert(18446744073709551616i32 == 0, ""); // expected-error {{integer 
literal is too large to be represented in any integer type}}
+  static_assert(18446744073709551616i64 == 0, ""); // expected-error {{integer 
literal is too large to be represented in any integer type}}
+  static_assert(18446744073709551616ui8 == 0, ""); // expected-error {{integer 
literal is too large to be represented in any integer type}}
+  static_assert(18446744073709551616ui16 == 0, ""); // expected-error 
{{integer literal is too large to be represented in any integer type}}
+  static_assert(18446744073709551616ui32 == 0, ""); // expected-error 
{{integer literal is too large to be represented in any integer type}}
+  static_assert(18446744073709551616ui64 == 0, ""); // expected-error 
{{integer literal is too large to be represented in any integer type}}
+}


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

Reply via email to