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// - `libc.src.errno.errno` is linked automatically by test rules
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// - `libc.src.errno.errno` is linked automatically by test rules
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; `libc_errno` is a no-op returning 0, while public
31// `<errno.h>` macro uses get a link-time failure.
32#define LIBC_ERRNO_MODE_UNDEFINED 1
33// libc maintains per-thread state (requires C++ `thread_local` support).
34#define LIBC_ERRNO_MODE_THREAD_LOCAL 2
35// libc maintains shared state used by all threads, contrary to standard C
36// semantics unless always single-threaded; nothing prevents data races.
37#define LIBC_ERRNO_MODE_SHARED 3
38// libc doesn't maintain any internal state, instead the embedder must define
39// `int *__llvm_libc_errno(void);` C function.
40#define LIBC_ERRNO_MODE_EXTERNAL 4
41// DEPRECATED: #define LIBC_ERRNO_MODE_SYSTEM 5
42// In this mode, the libc_errno is simply a macro resolved to `errno` from the
43// system header <errno.h>. There is no need to link against the
44// `libc.src.errno.errno` object, and public C++ symbol
45// `LIBC_NAMESPACE::libc_errno` doesn't exist.
46#define LIBC_ERRNO_MODE_SYSTEM_INLINE 6
47
48#if !defined(LIBC_ERRNO_MODE) || LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_DEFAULT
49#undef LIBC_ERRNO_MODE
50#if defined(LIBC_FULL_BUILD) || !defined(LIBC_COPT_PUBLIC_PACKAGING)
51#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_THREAD_LOCAL
52#else
53#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_SYSTEM_INLINE
54#endif
55#endif // LIBC_ERRNO_MODE
56
57#if LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_DEFAULT && \
58 LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_UNDEFINED && \
59 LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_THREAD_LOCAL && \
60 LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SHARED && \
61 LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_EXTERNAL && \
62 LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM_INLINE
63#error LIBC_ERRNO_MODE must be one of the following values: \
64LIBC_ERRNO_MODE_DEFAULT, \
65LIBC_ERRNO_MODE_UNDEFINED, \
66LIBC_ERRNO_MODE_THREAD_LOCAL, \
67LIBC_ERRNO_MODE_SHARED, \
68LIBC_ERRNO_MODE_EXTERNAL, \
69LIBC_ERRNO_MODE_SYSTEM_INLINE.
70#endif
71
72#if LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_SYSTEM_INLINE
73
74#include <errno.h>
75
76#define libc_errno errno
77
78#else // !LIBC_ERRNO_MODE_SYSTEM_INLINE
79
80#include "hdr/errno_macros.h"
81#include "src/__support/macros/attributes.h"
82#include "src/__support/macros/config.h"
83
84namespace LIBC_NAMESPACE_DECL {
85
86extern "C" int *__llvm_libc_errno() noexcept;
87
88struct Errno {
89#if LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_UNDEFINED
90 LIBC_INLINE constexpr void operator=(int) const noexcept {}
91 LIBC_INLINE constexpr operator int() const noexcept { return 0; }
92 LIBC_INLINE constexpr int *operator&() const noexcept { return nullptr; }
93#else
94 LIBC_INLINE void operator=(int a) const noexcept { *__llvm_libc_errno() = a; }
95 LIBC_INLINE operator int() const noexcept { return *__llvm_libc_errno(); }
96 LIBC_INLINE int *operator&() const noexcept { return __llvm_libc_errno(); }
97#endif
98};
99
100LIBC_INLINE_VAR constexpr Errno libc_errno{};
101
102} // namespace LIBC_NAMESPACE_DECL
103
104using LIBC_NAMESPACE::libc_errno;
105
106#endif // LIBC_ERRNO_MODE_SYSTEM_INLINE
107
108#endif // LLVM_LIBC_SRC___SUPPORT_LIBC_ERRNO_H
109