Path: blob/main/contrib/llvm-project/libc/src/__support/libc_errno.h
213766 views
//===-- Implementation header for libc_errno --------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_LIBC_ERRNO_H9#define LLVM_LIBC_SRC___SUPPORT_LIBC_ERRNO_H1011// This header is to be consumed by internal implementations, in which all of12// them should refer to `libc_errno` instead of using `errno` directly from13// <errno.h> header.1415// Unit and hermetic tests should:16// - #include "src/__support/libc_errno.h"17// - NOT #include <errno.h>18// - Only use `libc_errno` in the code19// - Depend on libc.src.errno.errno2021// Integration tests should:22// - NOT #include "src/__support/libc_errno.h"23// - #include <errno.h>24// - Use regular `errno` in the code25// - Still depend on libc.src.errno.errno2627// libc uses a fallback default value, either system or thread local.28#define LIBC_ERRNO_MODE_DEFAULT 029// libc never stores a value; `errno` macro uses get link-time failure.30#define LIBC_ERRNO_MODE_UNDEFINED 131// libc maintains per-thread state (requires C++ `thread_local` support).32#define LIBC_ERRNO_MODE_THREAD_LOCAL 233// libc maintains shared state used by all threads, contrary to standard C34// semantics unless always single-threaded; nothing prevents data races.35#define LIBC_ERRNO_MODE_SHARED 336// libc doesn't maintain any internal state, instead the embedder must define37// `int *__llvm_libc_errno(void);` C function.38#define LIBC_ERRNO_MODE_EXTERNAL 439// libc uses system `<errno.h>` `errno` macro directly in the overlay mode; in40// fullbuild mode, effectively the same as `LIBC_ERRNO_MODE_EXTERNAL`.41// In this mode, the public C++ symbol `LIBC_NAMESPACE::libc_errno ` is still42// exported and get redirected to the system `errno` inside its implementation.4344// TODO: Investigate deprecating LIBC_ERRNO_MODE_SYSTEM in favor of45// LIBC_ERRNO_MODE_SYSTEM_INLINE.46// https://github.com/llvm/llvm-project/issues/14345447#define LIBC_ERRNO_MODE_SYSTEM 548// In this mode, the libc_errno is simply a macro resolved to `errno` from the49// system header <errno.h>. There is no need to link against the50// `libc.src.errno.errno` object.51#define LIBC_ERRNO_MODE_SYSTEM_INLINE 65253#if !defined(LIBC_ERRNO_MODE) || LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_DEFAULT54#undef LIBC_ERRNO_MODE55#if defined(LIBC_FULL_BUILD) || !defined(LIBC_COPT_PUBLIC_PACKAGING)56#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_THREAD_LOCAL57#else58#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_SYSTEM59#endif60#endif // LIBC_ERRNO_MODE6162#if LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_DEFAULT && \63LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_UNDEFINED && \64LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_THREAD_LOCAL && \65LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SHARED && \66LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_EXTERNAL && \67LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM && \68LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM_INLINE69#error LIBC_ERRNO_MODE must be one of the following values: \70LIBC_ERRNO_MODE_DEFAULT, \71LIBC_ERRNO_MODE_UNDEFINED, \72LIBC_ERRNO_MODE_THREAD_LOCAL, \73LIBC_ERRNO_MODE_SHARED, \74LIBC_ERRNO_MODE_EXTERNAL, \75LIBC_ERRNO_MODE_SYSTEM, \76LIBC_ERRNO_MODE_SYSTEM_INLINE.77#endif7879#if LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_SYSTEM_INLINE8081#include <errno.h>8283#define libc_errno errno8485#else // !LIBC_ERRNO_MODE_SYSTEM_INLINE8687#include "hdr/errno_macros.h"88#include "src/__support/macros/config.h"8990namespace LIBC_NAMESPACE_DECL {9192extern "C" int *__llvm_libc_errno() noexcept;9394struct Errno {95void operator=(int);96operator int();97};9899extern Errno libc_errno;100101} // namespace LIBC_NAMESPACE_DECL102103using LIBC_NAMESPACE::libc_errno;104105#endif // LIBC_ERRNO_MODE_SYSTEM_INLINE106107#endif // LLVM_LIBC_SRC___SUPPORT_LIBC_ERRNO_H108109110