Skip to content

[libc] Add C23 limits.h header. #78887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion libc/cmake/modules/LLVMLibCLibraryRules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,17 @@ function(create_header_library fq_target_name)
target_sources(${fq_target_name} INTERFACE ${ADD_HEADER_HDRS})
if(ADD_HEADER_DEPENDS)
add_dependencies(${fq_target_name} ${ADD_HEADER_DEPENDS})
target_link_libraries(${fq_target_name} INTERFACE ${ADD_HEADER_DEPENDS})

# `*.__copied_hdr__` is created only to copy the header files to the target
# location, not to be linked against.
set(link_lib "")
foreach(dep ${ADD_HEADER_DEPENDS})
if (NOT dep MATCHES "__copied_hdr__")
list(APPEND link_lib ${dep})
endif()
endforeach()
Comment on lines +177 to +184
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: is this exposing your newly added limits.h to the consumers of llvm-libc?

Copy link
Contributor Author

@lntue lntue Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it's not. It just removes those extra command-only targets from getting linked. We won't add limits.h to the consumers, only the "internal" include/llvm-libc-macros/limit-macros.h.


target_link_libraries(${fq_target_name} INTERFACE ${link_lib})
endif()
if(ADD_HEADER_COMPILE_OPTIONS)
target_compile_options(${fq_target_name} INTERFACE ${ADD_HEADER_COMPILE_OPTIONS})
Expand Down
1 change: 1 addition & 0 deletions libc/config/darwin/arm/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.fenv
libc.include.float
libc.include.inttypes
libc.include.limits
libc.include.math
libc.include.stdlib
libc.include.string
Expand Down
1 change: 1 addition & 0 deletions libc/config/darwin/x86_64/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(TARGET_PUBLIC_HEADERS
#libc.include.fenv
libc.include.float
libc.include.inttypes
libc.include.limits
libc.include.math
libc.include.stdlib
libc.include.string
Expand Down
1 change: 1 addition & 0 deletions libc/config/gpu/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.string
libc.include.float
libc.include.inttypes
libc.include.limits
libc.include.math
libc.include.fenv
libc.include.time
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.fenv
libc.include.float
libc.include.inttypes
libc.include.limits
libc.include.math
libc.include.pthread
libc.include.signal
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/riscv/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.fenv
libc.include.float
libc.include.inttypes
libc.include.limits
libc.include.math
libc.include.pthread
libc.include.sched
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.fenv
libc.include.float
libc.include.inttypes
libc.include.limits
libc.include.math
libc.include.pthread
libc.include.sched
Expand Down
8 changes: 8 additions & 0 deletions libc/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ add_gen_header(
.llvm-libc-macros.float_macros
)

add_gen_header(
limits
DEF_FILE limits.h.def
GEN_HDR limits.h
DEPENDS
.llvm-libc-macros.limits_macros
)

add_gen_header(
math
DEF_FILE math.h.def
Expand Down
14 changes: 14 additions & 0 deletions libc/include/limits.h.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//===-- C standard library header limits.h --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_LIMITS_H
#define LLVM_LIBC_LIMITS_H

#include <llvm-libc-macros/limits-macros.h>

#endif // LLVM_LIBC_LIMITS_H
6 changes: 6 additions & 0 deletions libc/include/llvm-libc-macros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ add_macro_header(
float-macros.h
)

add_macro_header(
limits_macros
HDR
limits-macros.h
)

add_macro_header(
math_macros
HDR
Expand Down
12 changes: 12 additions & 0 deletions libc/include/llvm-libc-macros/float-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@
#ifndef __LLVM_LIBC_MACROS_FLOAT_MACROS_H
#define __LLVM_LIBC_MACROS_FLOAT_MACROS_H

// Suppress `#include_next is a language extension` warnings.
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu-include-next"
#else // gcc
#pragma GCC system_header
#endif //__clang__

#include_next <float.h>

#ifdef __clang__
#pragma clang diagnostic pop
#endif //__clang__

#ifndef FLT_RADIX
#define FLT_RADIX __FLT_RADIX__
#endif // FLT_RADIX
Expand Down
226 changes: 226 additions & 0 deletions libc/include/llvm-libc-macros/limits-macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
//===-- Definition of macros from limits.h --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef __LLVM_LIBC_MACROS_LIMITS_MACROS_H
#define __LLVM_LIBC_MACROS_LIMITS_MACROS_H

// Define all C23 macro constants of limits.h

#ifndef CHAR_BIT
#ifdef __CHAR_BIT__
#define CHAR_BIT __CHAR_BIT__
#else
#define CHAR_BIT 8
#endif // __CHAR_BIT__
#endif // CHAR_BIT

// TODO: https://github.com/llvm/llvm-project/issues/79358
// Define MB_LEN_MAX if missing.
// clang: MB_LEN_MAX = 1 -
// https://github.com/llvm/llvm-project/blob/main/clang/lib/Headers/limits.h#L64
// glibc: MB_LEN_MAX = 16 -
// https://github.com/bminor/glibc/blob/master/include/limits.h#L32

// *_WIDTH macros

#ifndef CHAR_WIDTH
#define CHAR_WIDTH CHAR_BIT
#endif // CHAR_WIDTH

#ifndef SCHAR_WIDTH
#define SCHAR_WIDTH CHAR_BIT
#endif // SCHAR_WIDTH

#ifndef UCHAR_WIDTH
#define UCHAR_WIDTH CHAR_BIT
#endif // UCHAR_WIDTH

#ifndef SHRT_WIDTH
#ifdef __SHRT_WIDTH__
#define SHRT_WIDTH __SHRT_WIDTH__
#else
#define SHRT_WIDTH 16
#endif // __SHRT_WIDTH__
#endif // SHRT_WIDTH

#ifndef USHRT_WIDTH
#define USHRT_WIDTH SHRT_WIDTH
#endif // USHRT_WIDTH

#ifndef INT_WIDTH
#ifdef __INT_WIDTH__
#define INT_WIDTH __INT_WIDTH__
#else
#define INT_WIDTH 32
#endif // __INT_WIDTH__
#endif // INT_WIDTH

#ifndef UINT_WIDTH
#define UINT_WIDTH INT_WIDTH
#endif // UINT_WIDTH

#ifndef LONG_WIDTH
#ifdef __LONG_WIDTH__
#define LONG_WIDTH __LONG_WIDTH__
#elif defined(__WORDSIZE)
#define LONG_WIDTH __WORDSIZE
#else
#error "Unknown WORDSIZE to define LONG_WIDTH."
#endif // __LONG_WIDTH__
#endif // LONG_WIDTH

#ifndef ULONG_WIDTH
#define ULONG_WIDTH LONG_WIDTH
#endif // ULONG_WIDTH

#ifndef LLONG_WIDTH
#ifdef __LLONG_WIDTH__
#define LLONG_WIDTH __LLONG_WIDTH__
#else
#define LLONG_WIDTH 64
#endif // __LLONG_WIDTH__
#endif // LLONG_WIDTH

#ifndef ULLONG_WIDTH
#define ULLONG_WIDTH LLONG_WIDTH
#endif // ULLONG_WIDTH

#ifndef BOOL_WIDTH
#ifdef __BOOL_WIDTH__
#define BOOL_WIDTH __BOOL_WIDTH__
#else
#define BOOL_WIDTH 1
#endif // __BOOL_WIDTH__
#endif // BOOL_WIDTH

// *_MAX macros

#ifndef SCHAR_MAX
#ifdef __SCHAR_MAX__
#define SCHAR_MAX __SCHAR_MAX__
#else
#define SCHAR_MAX 0x7f
#endif // __SCHAR_MAX__
#endif // SCHAR_MAX

#ifndef UCHAR_MAX
#define UCHAR_MAX (SCHAR_MAX * 2 + 1)
#endif // UCHAR_MAX

// Check if char is unsigned.
#if !defined(__CHAR_UNSIGNED__) && ('\xff' > 0)
#define __CHAR_UNSIGNED__
#endif

#ifndef CHAR_MAX
#ifdef __CHAR_UNSIGNED__
#define CHAR_MAX UCHAR_MAX
#else
#define CHAR_MAX SCHAR_MAX
#endif // __CHAR_UNSIGNED__
#endif // CHAR_MAX

#ifndef SHRT_MAX
#ifdef __SHRT_MAX__
#define SHRT_MAX __SHRT_MAX__
#else
#define SHRT_MAX 0x7fff
#endif // __SHRT_MAX__
#endif // SHRT_MAX

#ifndef USHRT_MAX
#define USHRT_MAX (SHRT_MAX * 2U + 1U)
#endif // USHRT_MAX

#ifndef INT_MAX
#ifdef __INT_MAX__
#define INT_MAX __INT_MAX__
#else
#define INT_MAX (0 ^ (1 << (INT_WIDTH - 1)))
#endif // __INT_MAX__
#endif // INT_MAX

#ifndef UINT_MAX
#define UINT_MAX (~0U)
#endif // UINT_MAX

#ifndef LONG_MAX
#ifdef __LONG_MAX__
#define LONG_MAX __LONG_MAX__
#else
#define LONG_MAX (0L ^ (1L << (LONG_WIDTH - 1)))
#endif // __LONG_MAX__
#endif // LONG_MAX

#ifndef ULONG_MAX
#define ULONG_MAX (~0UL)
#endif // ULONG_MAX

#ifndef LLONG_MAX
#ifdef __LONG_LONG_MAX__
#define LLONG_MAX __LONG_LONG_MAX__
#else
#define LLONG_MAX (0LL ^ (1LL << (LLONG_WIDTH - 1)))
#endif // __LONG_LONG_MAX__
#endif // LLONG_MAX

#ifndef ULLONG_MAX
#define ULLONG_MAX (~0ULL)
#endif // ULLONG_MAX

// *_MIN macros

#ifndef SCHAR_MIN
#define SCHAR_MIN (-SCHAR_MAX - 1)
#endif // SCHAR_MIN

#ifndef UCHAR_MIN
#define UCHAR_MIN 0
#endif // UCHAR_MIN

#ifndef CHAR_MIN
#ifdef __CHAR_UNSIGNED__
#define CHAR_MIN UCHAR_MIN
#else
#define CHAR_MIN SCHAR_MIN
#endif // __CHAR_UNSIGNED__
#endif // CHAR_MIN

#ifndef SHRT_MIN
#define SHRT_MIN (-SHRT_MAX - 1)
#endif // SHRT_MIN

#ifndef USHRT_MIN
#define USHRT_MIN 0U
#endif // USHRT_MIN

#ifndef INT_MIN
#define INT_MIN (-INT_MAX - 1)
#endif // INT_MIN

#ifndef UINT_MIN
#define UINT_MIN 0U
#endif // UINT_MIN

#ifndef LONG_MIN
#define LONG_MIN (-LONG_MAX - 1L)
#endif // LONG_MIN

#ifndef ULONG_MIN
#define ULONG_MIN 0UL
#endif // ULONG_MIN

#ifndef LLONG_MIN
#define LLONG_MIN (-LLONG_MAX - 1LL)
#endif // LLONG_MIN

#ifndef ULLONG_MIN
#define ULLONG_MIN 0ULL
#endif // ULLONG_MIN

#endif // __LLVM_LIBC_MACROS_LIMITS_MACROS_H
3 changes: 3 additions & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,8 @@ def StdC : StandardSpec<"stdc"> {
]
>;

HeaderSpec Limits = HeaderSpec<"limits.h">;

NamedType SigAtomicT = NamedType<"sig_atomic_t">;
HeaderSpec Signal = HeaderSpec<
"signal.h",
Expand Down Expand Up @@ -1159,6 +1161,7 @@ def StdC : StandardSpec<"stdc"> {
Errno,
Fenv,
Float,
Limits,
Math,
String,
StdIO,
Expand Down
1 change: 1 addition & 0 deletions libc/src/__support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ add_header_library(
HDRS
math_extras.h
DEPENDS
libc.src.__support.CPP.limits
libc.src.__support.CPP.type_traits
libc.src.__support.macros.attributes
libc.src.__support.macros.config
Expand Down
1 change: 1 addition & 0 deletions libc/src/__support/CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ add_header_library(
limits.h
DEPENDS
.type_traits
libc.include.llvm-libc-macros.limits_macros
)

add_header_library(
Expand Down
Loading