Skip to content

Commit 72ce629

Browse files
authored
[libc] Add C23 limits.h header. (#78887)
1 parent fe9f390 commit 72ce629

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+318
-39
lines changed

libc/cmake/modules/LLVMLibCLibraryRules.cmake

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,17 @@ function(create_header_library fq_target_name)
173173
target_sources(${fq_target_name} INTERFACE ${ADD_HEADER_HDRS})
174174
if(ADD_HEADER_DEPENDS)
175175
add_dependencies(${fq_target_name} ${ADD_HEADER_DEPENDS})
176-
target_link_libraries(${fq_target_name} INTERFACE ${ADD_HEADER_DEPENDS})
176+
177+
# `*.__copied_hdr__` is created only to copy the header files to the target
178+
# location, not to be linked against.
179+
set(link_lib "")
180+
foreach(dep ${ADD_HEADER_DEPENDS})
181+
if (NOT dep MATCHES "__copied_hdr__")
182+
list(APPEND link_lib ${dep})
183+
endif()
184+
endforeach()
185+
186+
target_link_libraries(${fq_target_name} INTERFACE ${link_lib})
177187
endif()
178188
if(ADD_HEADER_COMPILE_OPTIONS)
179189
target_compile_options(${fq_target_name} INTERFACE ${ADD_HEADER_COMPILE_OPTIONS})

libc/config/darwin/arm/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(TARGET_PUBLIC_HEADERS
44
libc.include.fenv
55
libc.include.float
66
libc.include.inttypes
7+
libc.include.limits
78
libc.include.math
89
libc.include.stdlib
910
libc.include.string

libc/config/darwin/x86_64/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(TARGET_PUBLIC_HEADERS
55
#libc.include.fenv
66
libc.include.float
77
libc.include.inttypes
8+
libc.include.limits
89
libc.include.math
910
libc.include.stdlib
1011
libc.include.string

libc/config/gpu/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(TARGET_PUBLIC_HEADERS
44
libc.include.string
55
libc.include.float
66
libc.include.inttypes
7+
libc.include.limits
78
libc.include.math
89
libc.include.fenv
910
libc.include.time

libc/config/linux/aarch64/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(TARGET_PUBLIC_HEADERS
66
libc.include.fenv
77
libc.include.float
88
libc.include.inttypes
9+
libc.include.limits
910
libc.include.math
1011
libc.include.pthread
1112
libc.include.signal

libc/config/linux/riscv/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set(TARGET_PUBLIC_HEADERS
88
libc.include.fenv
99
libc.include.float
1010
libc.include.inttypes
11+
libc.include.limits
1112
libc.include.math
1213
libc.include.pthread
1314
libc.include.sched

libc/config/linux/x86_64/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set(TARGET_PUBLIC_HEADERS
88
libc.include.fenv
99
libc.include.float
1010
libc.include.inttypes
11+
libc.include.limits
1112
libc.include.math
1213
libc.include.pthread
1314
libc.include.sched

libc/include/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ add_gen_header(
8383
.llvm-libc-macros.float_macros
8484
)
8585

86+
add_gen_header(
87+
limits
88+
DEF_FILE limits.h.def
89+
GEN_HDR limits.h
90+
DEPENDS
91+
.llvm-libc-macros.limits_macros
92+
)
93+
8694
add_gen_header(
8795
math
8896
DEF_FILE math.h.def

libc/include/limits.h.def

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- C standard library header limits.h --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_LIMITS_H
10+
#define LLVM_LIBC_LIMITS_H
11+
12+
#include <llvm-libc-macros/limits-macros.h>
13+
14+
#endif // LLVM_LIBC_LIMITS_H

libc/include/llvm-libc-macros/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ add_macro_header(
7373
float-macros.h
7474
)
7575

76+
add_macro_header(
77+
limits_macros
78+
HDR
79+
limits-macros.h
80+
)
81+
7682
add_macro_header(
7783
math_macros
7884
HDR

libc/include/llvm-libc-macros/float-macros.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,20 @@
99
#ifndef __LLVM_LIBC_MACROS_FLOAT_MACROS_H
1010
#define __LLVM_LIBC_MACROS_FLOAT_MACROS_H
1111

12+
// Suppress `#include_next is a language extension` warnings.
13+
#ifdef __clang__
14+
#pragma clang diagnostic push
15+
#pragma clang diagnostic ignored "-Wgnu-include-next"
16+
#else // gcc
17+
#pragma GCC system_header
18+
#endif //__clang__
19+
1220
#include_next <float.h>
1321

22+
#ifdef __clang__
23+
#pragma clang diagnostic pop
24+
#endif //__clang__
25+
1426
#ifndef FLT_RADIX
1527
#define FLT_RADIX __FLT_RADIX__
1628
#endif // FLT_RADIX
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
//===-- Definition of macros from limits.h --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __LLVM_LIBC_MACROS_LIMITS_MACROS_H
10+
#define __LLVM_LIBC_MACROS_LIMITS_MACROS_H
11+
12+
// Define all C23 macro constants of limits.h
13+
14+
#ifndef CHAR_BIT
15+
#ifdef __CHAR_BIT__
16+
#define CHAR_BIT __CHAR_BIT__
17+
#else
18+
#define CHAR_BIT 8
19+
#endif // __CHAR_BIT__
20+
#endif // CHAR_BIT
21+
22+
// TODO: https://github.com/llvm/llvm-project/issues/79358
23+
// Define MB_LEN_MAX if missing.
24+
// clang: MB_LEN_MAX = 1 -
25+
// https://github.com/llvm/llvm-project/blob/main/clang/lib/Headers/limits.h#L64
26+
// glibc: MB_LEN_MAX = 16 -
27+
// https://github.com/bminor/glibc/blob/master/include/limits.h#L32
28+
29+
// *_WIDTH macros
30+
31+
#ifndef CHAR_WIDTH
32+
#define CHAR_WIDTH CHAR_BIT
33+
#endif // CHAR_WIDTH
34+
35+
#ifndef SCHAR_WIDTH
36+
#define SCHAR_WIDTH CHAR_BIT
37+
#endif // SCHAR_WIDTH
38+
39+
#ifndef UCHAR_WIDTH
40+
#define UCHAR_WIDTH CHAR_BIT
41+
#endif // UCHAR_WIDTH
42+
43+
#ifndef SHRT_WIDTH
44+
#ifdef __SHRT_WIDTH__
45+
#define SHRT_WIDTH __SHRT_WIDTH__
46+
#else
47+
#define SHRT_WIDTH 16
48+
#endif // __SHRT_WIDTH__
49+
#endif // SHRT_WIDTH
50+
51+
#ifndef USHRT_WIDTH
52+
#define USHRT_WIDTH SHRT_WIDTH
53+
#endif // USHRT_WIDTH
54+
55+
#ifndef INT_WIDTH
56+
#ifdef __INT_WIDTH__
57+
#define INT_WIDTH __INT_WIDTH__
58+
#else
59+
#define INT_WIDTH 32
60+
#endif // __INT_WIDTH__
61+
#endif // INT_WIDTH
62+
63+
#ifndef UINT_WIDTH
64+
#define UINT_WIDTH INT_WIDTH
65+
#endif // UINT_WIDTH
66+
67+
#ifndef LONG_WIDTH
68+
#ifdef __LONG_WIDTH__
69+
#define LONG_WIDTH __LONG_WIDTH__
70+
#elif defined(__WORDSIZE)
71+
#define LONG_WIDTH __WORDSIZE
72+
#else
73+
#error "Unknown WORDSIZE to define LONG_WIDTH."
74+
#endif // __LONG_WIDTH__
75+
#endif // LONG_WIDTH
76+
77+
#ifndef ULONG_WIDTH
78+
#define ULONG_WIDTH LONG_WIDTH
79+
#endif // ULONG_WIDTH
80+
81+
#ifndef LLONG_WIDTH
82+
#ifdef __LLONG_WIDTH__
83+
#define LLONG_WIDTH __LLONG_WIDTH__
84+
#else
85+
#define LLONG_WIDTH 64
86+
#endif // __LLONG_WIDTH__
87+
#endif // LLONG_WIDTH
88+
89+
#ifndef ULLONG_WIDTH
90+
#define ULLONG_WIDTH LLONG_WIDTH
91+
#endif // ULLONG_WIDTH
92+
93+
#ifndef BOOL_WIDTH
94+
#ifdef __BOOL_WIDTH__
95+
#define BOOL_WIDTH __BOOL_WIDTH__
96+
#else
97+
#define BOOL_WIDTH 1
98+
#endif // __BOOL_WIDTH__
99+
#endif // BOOL_WIDTH
100+
101+
// *_MAX macros
102+
103+
#ifndef SCHAR_MAX
104+
#ifdef __SCHAR_MAX__
105+
#define SCHAR_MAX __SCHAR_MAX__
106+
#else
107+
#define SCHAR_MAX 0x7f
108+
#endif // __SCHAR_MAX__
109+
#endif // SCHAR_MAX
110+
111+
#ifndef UCHAR_MAX
112+
#define UCHAR_MAX (SCHAR_MAX * 2 + 1)
113+
#endif // UCHAR_MAX
114+
115+
// Check if char is unsigned.
116+
#if !defined(__CHAR_UNSIGNED__) && ('\xff' > 0)
117+
#define __CHAR_UNSIGNED__
118+
#endif
119+
120+
#ifndef CHAR_MAX
121+
#ifdef __CHAR_UNSIGNED__
122+
#define CHAR_MAX UCHAR_MAX
123+
#else
124+
#define CHAR_MAX SCHAR_MAX
125+
#endif // __CHAR_UNSIGNED__
126+
#endif // CHAR_MAX
127+
128+
#ifndef SHRT_MAX
129+
#ifdef __SHRT_MAX__
130+
#define SHRT_MAX __SHRT_MAX__
131+
#else
132+
#define SHRT_MAX 0x7fff
133+
#endif // __SHRT_MAX__
134+
#endif // SHRT_MAX
135+
136+
#ifndef USHRT_MAX
137+
#define USHRT_MAX (SHRT_MAX * 2U + 1U)
138+
#endif // USHRT_MAX
139+
140+
#ifndef INT_MAX
141+
#ifdef __INT_MAX__
142+
#define INT_MAX __INT_MAX__
143+
#else
144+
#define INT_MAX (0 ^ (1 << (INT_WIDTH - 1)))
145+
#endif // __INT_MAX__
146+
#endif // INT_MAX
147+
148+
#ifndef UINT_MAX
149+
#define UINT_MAX (~0U)
150+
#endif // UINT_MAX
151+
152+
#ifndef LONG_MAX
153+
#ifdef __LONG_MAX__
154+
#define LONG_MAX __LONG_MAX__
155+
#else
156+
#define LONG_MAX (0L ^ (1L << (LONG_WIDTH - 1)))
157+
#endif // __LONG_MAX__
158+
#endif // LONG_MAX
159+
160+
#ifndef ULONG_MAX
161+
#define ULONG_MAX (~0UL)
162+
#endif // ULONG_MAX
163+
164+
#ifndef LLONG_MAX
165+
#ifdef __LONG_LONG_MAX__
166+
#define LLONG_MAX __LONG_LONG_MAX__
167+
#else
168+
#define LLONG_MAX (0LL ^ (1LL << (LLONG_WIDTH - 1)))
169+
#endif // __LONG_LONG_MAX__
170+
#endif // LLONG_MAX
171+
172+
#ifndef ULLONG_MAX
173+
#define ULLONG_MAX (~0ULL)
174+
#endif // ULLONG_MAX
175+
176+
// *_MIN macros
177+
178+
#ifndef SCHAR_MIN
179+
#define SCHAR_MIN (-SCHAR_MAX - 1)
180+
#endif // SCHAR_MIN
181+
182+
#ifndef UCHAR_MIN
183+
#define UCHAR_MIN 0
184+
#endif // UCHAR_MIN
185+
186+
#ifndef CHAR_MIN
187+
#ifdef __CHAR_UNSIGNED__
188+
#define CHAR_MIN UCHAR_MIN
189+
#else
190+
#define CHAR_MIN SCHAR_MIN
191+
#endif // __CHAR_UNSIGNED__
192+
#endif // CHAR_MIN
193+
194+
#ifndef SHRT_MIN
195+
#define SHRT_MIN (-SHRT_MAX - 1)
196+
#endif // SHRT_MIN
197+
198+
#ifndef USHRT_MIN
199+
#define USHRT_MIN 0U
200+
#endif // USHRT_MIN
201+
202+
#ifndef INT_MIN
203+
#define INT_MIN (-INT_MAX - 1)
204+
#endif // INT_MIN
205+
206+
#ifndef UINT_MIN
207+
#define UINT_MIN 0U
208+
#endif // UINT_MIN
209+
210+
#ifndef LONG_MIN
211+
#define LONG_MIN (-LONG_MAX - 1L)
212+
#endif // LONG_MIN
213+
214+
#ifndef ULONG_MIN
215+
#define ULONG_MIN 0UL
216+
#endif // ULONG_MIN
217+
218+
#ifndef LLONG_MIN
219+
#define LLONG_MIN (-LLONG_MAX - 1LL)
220+
#endif // LLONG_MIN
221+
222+
#ifndef ULLONG_MIN
223+
#define ULLONG_MIN 0ULL
224+
#endif // ULLONG_MIN
225+
226+
#endif // __LLVM_LIBC_MACROS_LIMITS_MACROS_H

libc/spec/stdc.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,8 @@ def StdC : StandardSpec<"stdc"> {
857857
]
858858
>;
859859

860+
HeaderSpec Limits = HeaderSpec<"limits.h">;
861+
860862
NamedType SigAtomicT = NamedType<"sig_atomic_t">;
861863
HeaderSpec Signal = HeaderSpec<
862864
"signal.h",
@@ -1160,6 +1162,7 @@ def StdC : StandardSpec<"stdc"> {
11601162
Errno,
11611163
Fenv,
11621164
Float,
1165+
Limits,
11631166
Math,
11641167
String,
11651168
StdIO,

libc/src/__support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ add_header_library(
3434
HDRS
3535
math_extras.h
3636
DEPENDS
37+
libc.src.__support.CPP.limits
3738
libc.src.__support.CPP.type_traits
3839
libc.src.__support.macros.attributes
3940
libc.src.__support.macros.config

libc/src/__support/CPP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ add_header_library(
4848
limits.h
4949
DEPENDS
5050
.type_traits
51+
libc.include.llvm-libc-macros.limits_macros
5152
)
5253

5354
add_header_library(

0 commit comments

Comments
 (0)