[clang] [Clang] Define `__WCHAR_MIN__` and related macros (PR #208135)
Zeyi Xu via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 8 08:36:07 PDT 2026
https://github.com/zeyi2 updated https://github.com/llvm/llvm-project/pull/208135
>From 6bacea1432e5e8862afb8826d30fa401f44c50d0 Mon Sep 17 00:00:00 2001
From: Zeyi Xu <mitchell.xu2 at gmail.com>
Date: Wed, 8 Jul 2026 11:14:17 +0800
Subject: [PATCH 1/2] [Clang] Define __WCHAR_MIN__ and related macros
---
clang/lib/Frontend/InitPreprocessor.cpp | 11 +++++++++++
clang/test/Preprocessor/init-aarch64.c | 3 +++
clang/test/Preprocessor/init-riscv.c | 8 ++++++++
clang/test/Preprocessor/init.c | 4 ++++
4 files changed, 26 insertions(+)
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index 7752ab5131aae..8b6ff844d0daa 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -186,6 +186,14 @@ static void DefineTypeSize(const Twine &MacroName, TargetInfo::IntType Ty,
TI.isTypeSigned(Ty), Builder);
}
+static void DefineTypeMin(const Twine &Prefix, TargetInfo::IntType Ty,
+ const TargetInfo &TI, MacroBuilder &Builder) {
+ Builder.defineMacro(Prefix + "_MIN__",
+ TI.isTypeSigned(Ty)
+ ? Twine("(-") + Prefix + "_MAX__ - 1)"
+ : Twine("0") + TI.getTypeConstantSuffix(Ty));
+}
+
static void DefineFmt(const LangOptions &LangOpts, const Twine &Prefix,
TargetInfo::IntType Ty, const TargetInfo &TI,
MacroBuilder &Builder) {
@@ -1129,7 +1137,9 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder);
DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder);
DefineTypeSizeAndWidth("__WCHAR", TI.getWCharType(), TI, Builder);
+ DefineTypeMin("__WCHAR", TI.getWCharType(), TI, Builder);
DefineTypeSizeAndWidth("__WINT", TI.getWIntType(), TI, Builder);
+ DefineTypeMin("__WINT", TI.getWIntType(), TI, Builder);
DefineTypeSizeAndWidth("__INTMAX", TI.getIntMaxType(), TI, Builder);
DefineTypeSizeAndWidth("__SIZE", TI.getSizeType(), TI, Builder);
@@ -1182,6 +1192,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder);
DefineType("__WINT_TYPE__", TI.getWIntType(), Builder);
DefineTypeSizeAndWidth("__SIG_ATOMIC", TI.getSigAtomicType(), TI, Builder);
+ DefineTypeMin("__SIG_ATOMIC", TI.getSigAtomicType(), TI, Builder);
if (LangOpts.C23)
DefineType("__CHAR8_TYPE__", TI.UnsignedChar, Builder);
DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder);
diff --git a/clang/test/Preprocessor/init-aarch64.c b/clang/test/Preprocessor/init-aarch64.c
index 09e3fc926a309..f875c10775e29 100644
--- a/clang/test/Preprocessor/init-aarch64.c
+++ b/clang/test/Preprocessor/init-aarch64.c
@@ -263,6 +263,7 @@
// AARCH64-NEXT: #define __SHRT_MAX__ 32767
// AARCH64-NEXT: #define __SHRT_WIDTH__ 16
// AARCH64-NEXT: #define __SIG_ATOMIC_MAX__ 2147483647
+// AARCH64-NEXT: #define __SIG_ATOMIC_MIN__ (-__SIG_ATOMIC_MAX__ - 1)
// AARCH64-NEXT: #define __SIG_ATOMIC_WIDTH__ 32
// AARCH64-NEXT: #define __SIZEOF_DOUBLE__ 8
// AARCH64-NEXT: #define __SIZEOF_FLOAT__ 4
@@ -393,10 +394,12 @@
// AARCH64-NEXT: #define __USER_LABEL_PREFIX__
// AARCH64-NEXT: #define __VERSION__ "{{.*}}"
// AARCH64-NEXT: #define __WCHAR_MAX__ 4294967295U
+// AARCH64-NEXT: #define __WCHAR_MIN__ 0U
// AARCH64-NEXT: #define __WCHAR_TYPE__ unsigned int
// AARCH64-NEXT: #define __WCHAR_UNSIGNED__ 1
// AARCH64-NEXT: #define __WCHAR_WIDTH__ 32
// AARCH64-NEXT: #define __WINT_MAX__ 2147483647
+// AARCH64-NEXT: #define __WINT_MIN__ (-__WINT_MAX__ - 1)
// AARCH64-NEXT: #define __WINT_TYPE__ int
// AARCH64-NEXT: #define __WINT_WIDTH__ 32
// AARCH64-NEXT: #define __aarch64__ 1
diff --git a/clang/test/Preprocessor/init-riscv.c b/clang/test/Preprocessor/init-riscv.c
index 4eeecccff4378..00282e670a0f7 100644
--- a/clang/test/Preprocessor/init-riscv.c
+++ b/clang/test/Preprocessor/init-riscv.c
@@ -2,9 +2,17 @@
// RUN: FileCheck -match-full-lines -check-prefixes=RV32 %s
// RUN: %clang_cc1 -E -dM -triple=riscv64 < /dev/null | \
// RUN: FileCheck -match-full-lines -check-prefixes=RV64 %s
+// RUN: %clang_cc1 -E -dM -triple=riscv32-unknown-netbsd < /dev/null | \
+// RUN: FileCheck -match-full-lines -check-prefixes=NETBSD %s
+// RUN: %clang_cc1 -E -dM -triple=riscv64-unknown-netbsd < /dev/null | \
+// RUN: FileCheck -match-full-lines -check-prefixes=NETBSD %s
// RV32: #define __GCC_CONSTRUCTIVE_SIZE 64
// RV32: #define __GCC_DESTRUCTIVE_SIZE 64
// RV64: #define __GCC_CONSTRUCTIVE_SIZE 64
// RV64: #define __GCC_DESTRUCTIVE_SIZE 64
+
+// NETBSD: #define __SIG_ATOMIC_MIN__ (-__SIG_ATOMIC_MAX__ - 1)
+// NETBSD: #define __WCHAR_MIN__ (-__WCHAR_MAX__ - 1)
+// NETBSD: #define __WINT_MIN__ 0U
diff --git a/clang/test/Preprocessor/init.c b/clang/test/Preprocessor/init.c
index ef7b76a29a1f5..afea10ea2a38f 100644
--- a/clang/test/Preprocessor/init.c
+++ b/clang/test/Preprocessor/init.c
@@ -1949,8 +1949,10 @@
// WEBASSEMBLY-NEXT:#define __SHRT_MAX__ 32767
// WEBASSEMBLY-NEXT:#define __SHRT_WIDTH__ 16
// WEBASSEMBLY32-NEXT:#define __SIG_ATOMIC_MAX__ 2147483647L
+// WEBASSEMBLY32-NEXT:#define __SIG_ATOMIC_MIN__ (-__SIG_ATOMIC_MAX__ - 1)
// WEBASSEMBLY32-NEXT:#define __SIG_ATOMIC_WIDTH__ 32
// WEBASSEMBLY64-NEXT:#define __SIG_ATOMIC_MAX__ 9223372036854775807L
+// WEBASSEMBLY64-NEXT:#define __SIG_ATOMIC_MIN__ (-__SIG_ATOMIC_MAX__ - 1)
// WEBASSEMBLY64-NEXT:#define __SIG_ATOMIC_WIDTH__ 64
// WEBASSEMBLY-NEXT:#define __SIZEOF_DOUBLE__ 8
// WEBASSEMBLY-NEXT:#define __SIZEOF_FLOAT__ 4
@@ -2092,10 +2094,12 @@
// WEBASSEMBLY-NEXT:#define __USER_LABEL_PREFIX__
// WEBASSEMBLY-NEXT:#define __VERSION__ "{{.*}}"
// WEBASSEMBLY-NEXT:#define __WCHAR_MAX__ 2147483647
+// WEBASSEMBLY-NEXT:#define __WCHAR_MIN__ (-__WCHAR_MAX__ - 1)
// WEBASSEMBLY-NEXT:#define __WCHAR_TYPE__ int
// WEBASSEMBLY-NOT:#define __WCHAR_UNSIGNED__
// WEBASSEMBLY-NEXT:#define __WCHAR_WIDTH__ 32
// WEBASSEMBLY-NEXT:#define __WINT_MAX__ 2147483647
+// WEBASSEMBLY-NEXT:#define __WINT_MIN__ (-__WINT_MAX__ - 1)
// WEBASSEMBLY-NEXT:#define __WINT_TYPE__ int
// WEBASSEMBLY-NOT:#define __WINT_UNSIGNED__
// WEBASSEMBLY-NEXT:#define __WINT_WIDTH__ 32
>From ad2b9d96ee5c5279e16a2d25eb5e7202d57d9959 Mon Sep 17 00:00:00 2001
From: Zeyi Xu <mitchell.xu2 at gmail.com>
Date: Wed, 8 Jul 2026 23:33:45 +0800
Subject: [PATCH 2/2] address feedback :)
---
clang/docs/ReleaseNotes.md | 1 +
clang/lib/Headers/stdint.h | 19 +++++--------------
2 files changed, 6 insertions(+), 14 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index e12ae0b2eeed4..1811a0e2a9a42 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -781,6 +781,7 @@ latest release, please see the [Clang Web Site](https://clang.llvm.org) or the
- Fixed assertion failures involving code completion with delayed default arguments and exception specifications. (#GH200879)
- Fixed a regression where calling a function that takes a class-type parameter by value inside `decltype` of a concept could be incorrectly rejected when used as a non-type template argument. (#GH175831)
- Fixed a crash in the constant evaluator when an ill-formed array new-expression whose bound could not be determined (e.g. `new int[]()`) was used in a constant expression. (#GH200139)
+- Clang now defines the GCC-compatible predefined macros `__WCHAR_MIN__`, `__WINT_MIN__`, and `__SIG_ATOMIC_MIN__`. (#GH199678)
#### Bug Fixes to Compiler Builtins
diff --git a/clang/lib/Headers/stdint.h b/clang/lib/Headers/stdint.h
index 96c2ccace13d0..2471b6e3860a4 100644
--- a/clang/lib/Headers/stdint.h
+++ b/clang/lib/Headers/stdint.h
@@ -805,25 +805,16 @@ typedef __UINTMAX_TYPE__ uintmax_t;
#endif
/* C99 7.18.3 Limits of other integer types. */
-#define SIG_ATOMIC_MIN __INTN_MIN(__SIG_ATOMIC_WIDTH__)
-#define SIG_ATOMIC_MAX __INTN_MAX(__SIG_ATOMIC_WIDTH__)
-#ifdef __WINT_UNSIGNED__
-# define WINT_MIN __UINTN_C(__WINT_WIDTH__, 0)
-# define WINT_MAX __UINTN_MAX(__WINT_WIDTH__)
-#else
-# define WINT_MIN __INTN_MIN(__WINT_WIDTH__)
-# define WINT_MAX __INTN_MAX(__WINT_WIDTH__)
-#endif
+#define SIG_ATOMIC_MIN __SIG_ATOMIC_MIN__
+#define SIG_ATOMIC_MAX __SIG_ATOMIC_MAX__
+#define WINT_MIN __WINT_MIN__
+#define WINT_MAX __WINT_MAX__
#ifndef WCHAR_MAX
# define WCHAR_MAX __WCHAR_MAX__
#endif
#ifndef WCHAR_MIN
-# if __WCHAR_MAX__ == __INTN_MAX(__WCHAR_WIDTH__)
-# define WCHAR_MIN __INTN_MIN(__WCHAR_WIDTH__)
-# else
-# define WCHAR_MIN __UINTN_C(__WCHAR_WIDTH__, 0)
-# endif
+# define WCHAR_MIN __WCHAR_MIN__
#endif
/* 7.18.4.2 Macros for greatest-width integer constants. */
More information about the cfe-commits
mailing list