Skip to content

Commit d87eea3

Browse files
authored
[libc] Move libc_errno.h to libc/src/__support and make LIBC_ERRNO_MODE_SYSTEM to be header-only. (#143187)
This is the first step in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450
1 parent fb761aa commit d87eea3

File tree

397 files changed

+829
-783
lines changed

Some content is hidden

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

397 files changed

+829
-783
lines changed

libc/cmake/modules/LLVMLibCCompileOptionRules.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ function(_get_compile_options_from_config output_var)
106106
list(APPEND config_options "-DLIBC_MATH=${LIBC_CONF_MATH_OPTIMIZATIONS}")
107107
endif()
108108

109+
if(LIBC_CONF_ERRNO_MODE)
110+
set(APPEND config_options "-DLIBC_ERRNO_MODE=${LIBC_CONF_ERRNO_MODE}")
111+
endif()
112+
109113
set(${output_var} ${config_options} PARENT_SCOPE)
110114
endfunction(_get_compile_options_from_config)
111115

libc/config/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"errno": {
33
"LIBC_CONF_ERRNO_MODE": {
44
"value": "LIBC_ERRNO_MODE_DEFAULT",
5-
"doc": "The implementation used for errno, acceptable values are LIBC_ERRNO_MODE_DEFAULT, LIBC_ERRNO_MODE_UNDEFINED, LIBC_ERRNO_MODE_THREAD_LOCAL, LIBC_ERRNO_MODE_SHARED, LIBC_ERRNO_MODE_EXTERNAL, and LIBC_ERRNO_MODE_SYSTEM."
5+
"doc": "The implementation used for errno, acceptable values are LIBC_ERRNO_MODE_DEFAULT, LIBC_ERRNO_MODE_UNDEFINED, LIBC_ERRNO_MODE_THREAD_LOCAL, LIBC_ERRNO_MODE_SHARED, LIBC_ERRNO_MODE_EXTERNAL, LIBC_ERRNO_MODE_SYSTEM, and LIBC_ERRNO_MODE_SYSTEM_INLINE."
66
}
77
},
88
"printf": {

libc/docs/dev/code_style.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ test infrastructure itself can be affected. To avoid perturbing the unit test
101101
infrastructure around the setting of ``errno``, the following rules are to be
102102
followed:
103103

104-
#. A special macro named ``libc_errno`` defined in ``src/errno/libc_errno.h``
104+
#. A special macro named ``libc_errno`` defined in ``src/__support/libc_errno.h``
105105
should be used when setting ``errno`` from libc runtime code. For example,
106106
code to set ``errno`` to ``EINVAL`` should be:
107107

@@ -117,7 +117,7 @@ followed:
117117
`ErrorOr <https://github.com/llvm/llvm-project/blob/main/libc/src/__support/error_or.h>`_
118118
to return error values.
119119

120-
#. The header file ``src/errno/libc_errno.h`` is shipped as part of the target
120+
#. The header file ``src/__support/libc_errno.h`` is shipped as part of the target
121121
corresponding to the ``errno`` entrypoint ``libc.src.errno.errno``. We do
122122
not in general allow dependencies between entrypoints. However, the ``errno``
123123
entrypoint is the only exceptional entrypoint on which other entrypoints

libc/shared/fp_bits.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_SHARED_FP_BITS_H
1010
#define LLVM_LIBC_SHARED_FP_BITS_H
1111

12+
#include "libc_common.h"
1213
#include "src/__support/FPUtil/FPBits.h"
1314

1415
namespace LIBC_NAMESPACE_DECL {

libc/shared/libc_common.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- Common defines for sharing LLVM libc with LLVM projects -*- C++ -*-===//
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_SHARED_LIBC_COMMON_H
10+
#define LLVM_LIBC_SHARED_LIBC_COMMON_H
11+
12+
// Use system errno.
13+
#ifdef LIBC_ERRNO_MODE
14+
#if LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM_INLINE
15+
#error \
16+
"LIBC_ERRNO_MODE was set to something different from LIBC_ERRNO_MODE_SYSTEM_INLINE."
17+
#endif // LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM_INLINE
18+
#else
19+
#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_SYSTEM_INLINE
20+
#endif // LIBC_ERRNO_MODE
21+
22+
#ifndef LIBC_NAMESPACE
23+
#define LIBC_NAMESPACE __llvm_libc
24+
#endif // LIBC_NAMESPACE
25+
26+
#endif // LLVM_LIBC_SHARED_LIBC_COMMON_H

libc/shared/rpc_server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_SHARED_RPC_SERVER_H
1010
#define LLVM_LIBC_SHARED_RPC_SERVER_H
1111

12+
#include "libc_common.h"
1213
#include "src/__support/RPC/rpc_server.h"
1314

1415
namespace LIBC_NAMESPACE_DECL {

libc/shared/str_to_float.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_SHARED_STR_TO_FLOAT_H
1010
#define LLVM_LIBC_SHARED_STR_TO_FLOAT_H
1111

12+
#include "libc_common.h"
1213
#include "src/__support/str_to_float.h"
1314

1415
namespace LIBC_NAMESPACE_DECL {

libc/shared/str_to_integer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_SHARED_STR_TO_INTEGER_H
1010
#define LLVM_LIBC_SHARED_STR_TO_INTEGER_H
1111

12+
#include "libc_common.h"
1213
#include "src/__support/str_to_integer.h"
1314

1415
namespace LIBC_NAMESPACE_DECL {

libc/src/__support/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
add_subdirectory(CPP)
22
add_subdirectory(macros)
33

4+
add_header_library(
5+
libc_errno
6+
HDRS
7+
libc_errno.h
8+
DEPENDS
9+
libc.hdr.errno_macros
10+
libc.src.__support.macros.config
11+
)
12+
413
add_header_library(
514
block
615
HDRS

libc/src/__support/FPUtil/FEnvImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
#include "hdr/fenv_macros.h"
1313
#include "hdr/math_macros.h"
1414
#include "hdr/types/fenv_t.h"
15+
#include "src/__support/libc_errno.h"
1516
#include "src/__support/macros/attributes.h" // LIBC_INLINE
1617
#include "src/__support/macros/config.h"
1718
#include "src/__support/macros/properties/architectures.h"
18-
#include "src/errno/libc_errno.h"
1919

2020
#if defined(LIBC_TARGET_ARCH_IS_AARCH64) && defined(__ARM_FP)
2121
#if defined(__APPLE__)

libc/src/__support/File/dir.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#include "src/__support/CPP/mutex.h" // lock_guard
1212
#include "src/__support/CPP/new.h"
1313
#include "src/__support/error_or.h"
14+
#include "src/__support/libc_errno.h" // For error macros
1415
#include "src/__support/macros/config.h"
15-
#include "src/errno/libc_errno.h" // For error macros
1616

1717
namespace LIBC_NAMESPACE_DECL {
1818

libc/src/__support/File/file.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#include "hdr/types/off_t.h"
1414
#include "src/__support/CPP/new.h"
1515
#include "src/__support/CPP/span.h"
16+
#include "src/__support/libc_errno.h" // For error macros
1617
#include "src/__support/macros/config.h"
17-
#include "src/errno/libc_errno.h" // For error macros
1818

1919
namespace LIBC_NAMESPACE_DECL {
2020

libc/src/__support/File/linux/file.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include "src/__support/File/linux/lseekImpl.h"
1616
#include "src/__support/OSUtil/fcntl.h"
1717
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
18+
#include "src/__support/libc_errno.h" // For error macros
1819
#include "src/__support/macros/config.h"
19-
#include "src/errno/libc_errno.h" // For error macros
2020

2121
#include "hdr/fcntl_macros.h" // For mode_t and other flags to the open syscall
2222
#include <sys/stat.h> // For S_IS*, S_IF*, and S_IR* flags.

libc/src/__support/File/linux/lseekImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1414
#include "src/__support/common.h"
1515
#include "src/__support/error_or.h"
16+
#include "src/__support/libc_errno.h"
1617
#include "src/__support/macros/config.h"
17-
#include "src/errno/libc_errno.h"
1818

1919
#include <stdint.h> // For uint64_t.
2020
#include <sys/syscall.h> // For syscall numbers.

libc/src/__support/HashTable/randomness.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "src/__support/macros/attributes.h"
1515
#include "src/__support/macros/config.h"
1616
#if defined(LIBC_HASHTABLE_USE_GETRANDOM)
17-
#include "src/errno/libc_errno.h"
17+
#include "src/__support/libc_errno.h"
1818
#include "src/sys/random/getrandom.h"
1919
#endif
2020

libc/src/__support/OSUtil/linux/fcntl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include "hdr/types/struct_flock64.h"
1616
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1717
#include "src/__support/common.h"
18+
#include "src/__support/libc_errno.h"
1819
#include "src/__support/macros/config.h"
19-
#include "src/errno/libc_errno.h"
2020

2121
#include <stdarg.h>
2222
#include <sys/syscall.h> // For syscall numbers.

libc/src/__support/OSUtil/linux/vdso.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
#include "src/__support/CPP/array.h"
1212
#include "src/__support/CPP/optional.h"
1313
#include "src/__support/CPP/string_view.h"
14+
#include "src/__support/libc_errno.h"
1415
#include "src/__support/threads/callonce.h"
1516
#include "src/__support/threads/linux/futex_word.h"
16-
#include "src/errno/libc_errno.h"
1717
#include "src/sys/auxv/getauxval.h"
1818
#include <linux/auxvec.h>
1919

libc/src/__support/StringUtil/tables/linux_extension_errors.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#define LLVM_LIBC_SRC___SUPPORT_STRINGUTIL_TABLES_LINUX_EXTENSION_ERRORS_H
1111

1212
#include "src/__support/StringUtil/message_mapper.h"
13+
#include "src/__support/libc_errno.h"
1314
#include "src/__support/macros/config.h"
14-
#include "src/errno/libc_errno.h"
1515

1616
namespace LIBC_NAMESPACE_DECL {
1717

libc/src/__support/libc_errno.h

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//===-- Implementation header for libc_errno --------------------*- C++ -*-===//
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_SRC___SUPPORT_LIBC_ERRNO_H
10+
#define LLVM_LIBC_SRC___SUPPORT_LIBC_ERRNO_H
11+
12+
// This header is to be consumed by internal implementations, in which all of
13+
// them should refer to `libc_errno` instead of using `errno` directly from
14+
// <errno.h> header.
15+
16+
// Unit and hermetic tests should:
17+
// - #include "src/__support/libc_errno.h"
18+
// - NOT #include <errno.h>
19+
// - Only use `libc_errno` in the code
20+
// - Depend on libc.src.errno.errno
21+
22+
// Integration tests should:
23+
// - NOT #include "src/__support/libc_errno.h"
24+
// - #include <errno.h>
25+
// - Use regular `errno` in the code
26+
// - Still depend on libc.src.errno.errno
27+
28+
// libc uses a fallback default value, either system or thread local.
29+
#define LIBC_ERRNO_MODE_DEFAULT 0
30+
// libc never stores a value; `errno` macro uses get link-time failure.
31+
#define LIBC_ERRNO_MODE_UNDEFINED 1
32+
// libc maintains per-thread state (requires C++ `thread_local` support).
33+
#define LIBC_ERRNO_MODE_THREAD_LOCAL 2
34+
// libc maintains shared state used by all threads, contrary to standard C
35+
// semantics unless always single-threaded; nothing prevents data races.
36+
#define LIBC_ERRNO_MODE_SHARED 3
37+
// libc doesn't maintain any internal state, instead the embedder must define
38+
// `int *__llvm_libc_errno(void);` C function.
39+
#define LIBC_ERRNO_MODE_EXTERNAL 4
40+
// libc uses system `<errno.h>` `errno` macro directly in the overlay mode; in
41+
// fullbuild mode, effectively the same as `LIBC_ERRNO_MODE_EXTERNAL`.
42+
// In this mode, the public C++ symbol `LIBC_NAMESPACE::libc_errno ` is still
43+
// exported and get redirected to the system `errno` inside its implementation.
44+
45+
// TODO: Investigate deprecating LIBC_ERRNO_MODE_SYSTEM in favor of
46+
// LIBC_ERRNO_MODE_SYSTEM_INLINE.
47+
// https://github.com/llvm/llvm-project/issues/143454
48+
#define LIBC_ERRNO_MODE_SYSTEM 5
49+
// In this mode, the libc_errno is simply a macro resolved to `errno` from the
50+
// system header <errno.h>. There is no need to link against the
51+
// `libc.src.errno.errno` object.
52+
#define LIBC_ERRNO_MODE_SYSTEM_INLINE 6
53+
54+
#if !defined(LIBC_ERRNO_MODE) || LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_DEFAULT
55+
#undef LIBC_ERRNO_MODE
56+
#if defined(LIBC_FULL_BUILD) || !defined(LIBC_COPT_PUBLIC_PACKAGING)
57+
#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_THREAD_LOCAL
58+
#else
59+
#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_SYSTEM
60+
#endif
61+
#endif // LIBC_ERRNO_MODE
62+
63+
#if LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_DEFAULT && \
64+
LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_UNDEFINED && \
65+
LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_THREAD_LOCAL && \
66+
LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SHARED && \
67+
LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_EXTERNAL && \
68+
LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM && \
69+
LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM_INLINE
70+
#error LIBC_ERRNO_MODE must be one of the following values: \
71+
LIBC_ERRNO_MODE_DEFAULT, \
72+
LIBC_ERRNO_MODE_UNDEFINED, \
73+
LIBC_ERRNO_MODE_THREAD_LOCAL, \
74+
LIBC_ERRNO_MODE_SHARED, \
75+
LIBC_ERRNO_MODE_EXTERNAL, \
76+
LIBC_ERRNO_MODE_SYSTEM, \
77+
LIBC_ERRNO_MODE_SYSTEM_INLINE.
78+
#endif
79+
80+
#if LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_SYSTEM_INLINE
81+
82+
#include <errno.h>
83+
84+
#define libc_errno errno
85+
86+
#else // !LIBC_ERRNO_MODE_SYSTEM_INLINE
87+
88+
#include "hdr/errno_macros.h"
89+
#include "src/__support/macros/config.h"
90+
91+
namespace LIBC_NAMESPACE_DECL {
92+
93+
extern "C" int *__llvm_libc_errno() noexcept;
94+
95+
struct Errno {
96+
void operator=(int);
97+
operator int();
98+
};
99+
100+
extern Errno libc_errno;
101+
102+
} // namespace LIBC_NAMESPACE_DECL
103+
104+
using LIBC_NAMESPACE::libc_errno;
105+
106+
#endif // LIBC_ERRNO_MODE_SYSTEM_INLINE
107+
108+
#endif // LLVM_LIBC_SRC___SUPPORT_LIBC_ERRNO_H

libc/src/__support/threads/linux/thread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
#include "src/__support/OSUtil/syscall.h" // For syscall functions.
1515
#include "src/__support/common.h"
1616
#include "src/__support/error_or.h"
17+
#include "src/__support/libc_errno.h" // For error macros
1718
#include "src/__support/macros/config.h"
1819
#include "src/__support/threads/linux/futex_utils.h" // For FutexWordType
19-
#include "src/errno/libc_errno.h" // For error macros
2020

2121
#ifdef LIBC_TARGET_ARCH_IS_AARCH64
2222
#include <arm_acle.h>

libc/src/dirent/closedir.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
#include "src/__support/File/dir.h"
1212
#include "src/__support/common.h"
13+
#include "src/__support/libc_errno.h"
1314
#include "src/__support/macros/config.h"
14-
#include "src/errno/libc_errno.h"
1515

1616
#include <dirent.h>
1717

libc/src/dirent/opendir.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
#include "src/__support/File/dir.h"
1212
#include "src/__support/common.h"
13+
#include "src/__support/libc_errno.h"
1314
#include "src/__support/macros/config.h"
14-
#include "src/errno/libc_errno.h"
1515

1616
#include <dirent.h>
1717

libc/src/dirent/readdir.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
#include "src/__support/File/dir.h"
1212
#include "src/__support/common.h"
13+
#include "src/__support/libc_errno.h"
1314
#include "src/__support/macros/config.h"
14-
#include "src/errno/libc_errno.h"
1515

1616
#include <dirent.h>
1717

libc/src/errno/CMakeLists.txt

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
11
# If we are in full build mode, we will provide the errno definition ourselves,
22
# and if we are in overlay mode, we will just re-use the system's errno.
3-
# We are passing LIBC_FULL_BUILD flag in full build mode so that the
4-
# implementation of libc_errno will know if we are in full build mode or not.
5-
6-
# TODO: Move LIBC_FULL_BUILD flag to _get_common_compile_options.
7-
set(full_build_flag "")
8-
if(LLVM_LIBC_FULL_BUILD)
9-
set(full_build_flag "-DLIBC_FULL_BUILD")
10-
endif()
11-
12-
if(LIBC_CONF_ERRNO_MODE)
13-
set(errno_config_copts "-DLIBC_ERRNO_MODE=${LIBC_CONF_ERRNO_MODE}")
14-
endif()
153

164
add_entrypoint_object(
175
errno
186
SRCS
197
libc_errno.cpp
208
HDRS
21-
libc_errno.h # Include this
22-
COMPILE_OPTIONS
23-
${full_build_flag}
24-
${errno_config_copts}
9+
../__support/libc_errno.h
2510
DEPENDS
2611
libc.hdr.errno_macros
2712
libc.src.__support.common
13+
libc.src.__support.libc_errno
14+
libc.src.__support.macros.attributes
15+
libc.src.__support.macros.config
2816
)

0 commit comments

Comments
 (0)