[clang] [Clang] Fix overflow checking for Microsoft integer literals (PR #212737)
Aditya prakash jha via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 29 04:32:07 PDT 2026
https://github.com/AdityaOP007 updated https://github.com/llvm/llvm-project/pull/212737
>From fdc54e566c9f23477e390b48ab80a9eca3bd1dd6 Mon Sep 17 00:00:00 2001
From: Aditya Prakash Jha <adityaprakashjha321 at gmail.com>
Date: Wed, 29 Jul 2026 16:31:16 +0530
Subject: [PATCH] [Clang] Fix overflow checking for Microsoft integer literals
---
clang/lib/Sema/SemaExpr.cpp | 9 ++++++++-
clang/test/SemaCXX/ms_integer_suffix.cpp | 13 ++++++++++++-
2 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 67d9ac4ad5cff..3211487d44959 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -4072,7 +4072,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(BitsNeeded, (unsigned)Literal.MicrosoftInteger);
}
llvm::APInt ResultVal(BitsNeeded, 0);
@@ -4114,6 +4114,13 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) {
Ty = Context.getIntTypeForBitwidth(Width,
/*Signed=*/!Literal.isUnsigned);
}
+
+ bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
+ if (!ResultVal.isIntN(Width) ||
+ (!AllowUnsigned && ResultVal[Width - 1] != 0)) {
+ Diag(Tok.getLocation(), diag::err_integer_literal_too_large)
+ << Literal.isUnsigned;
+ }
}
// 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..46b18dcfc5ed2 100644
--- a/clang/test/SemaCXX/ms_integer_suffix.cpp
+++ b/clang/test/SemaCXX/ms_integer_suffix.cpp
@@ -1,5 +1,4 @@
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fms-extensions -verify %s
-// expected-no-diagnostics
#ifdef __SIZEOF_INT8__
static_assert(sizeof(0i8) == __SIZEOF_INT8__, "");
@@ -8,6 +7,18 @@ constexpr int f(char) { return 1; }
constexpr int f(signed char) { return 2; }
static_assert(f(0i8) == 1, "");
+
+constexpr auto v1 = 127i8;
+constexpr auto v2 = 128i8; // expected-error {{integer literal is too large to be represented in any integer type}}
+constexpr auto v3 = 255i8; // expected-error {{integer literal is too large to be represented in any integer type}}
+constexpr auto v4 = 256i8; // expected-error {{integer literal is too large to be represented in any integer type}}
+constexpr auto v5 = -128i8; // expected-error {{integer literal is too large to be represented in any integer type}}
+constexpr auto v6 = -255i8; // expected-error {{integer literal is too large to be represented in any integer type}}
+static_assert(-255i8 == -255, ""); // expected-error {{integer literal is too large to be represented in any integer type}}
+
+constexpr auto v7 = 0xFFi8;
+constexpr auto v8 = 255ui8;
+
#endif
#ifdef __SIZEOF_INT16__
static_assert(sizeof(0i16) == __SIZEOF_INT16__, "");
More information about the cfe-commits
mailing list