1//===-- Portable attributes -------------------------------------*- 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// This header file defines macros for declaring attributes for functions,
9// types, and variables.
10//
11// These macros are used within llvm-libc and allow the compiler to optimize,
12// where applicable, certain function calls.
13//
14// Most macros here are exposing GCC or Clang features, and are stubbed out for
15// other compilers.
16
17#ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_ATTRIBUTES_H
18#define LLVM_LIBC_SRC___SUPPORT_MACROS_ATTRIBUTES_H
19
20#include "config.h"
21#include "properties/architectures.h"
22
23#ifndef __has_attribute
24#define __has_attribute(x) 0
25#endif
26
27#define LIBC_INLINE inline
28#define LIBC_INLINE_VAR inline
29#define LIBC_INLINE_ASM __asm__ __volatile__
30#define LIBC_UNUSED __attribute__((unused))
31
32#ifndef LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED
33#define LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED \
34 (__has_builtin(__builtin_is_constant_evaluated))
35#endif // LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED
36
37// TODO: Remove the macro once Clang/LLVM bump their minimum compilers' version.
38// The reason for indirection is GCC is known to fail with constexpr qualified
39// functions that doesn't produce constant expression. This avoids it by using
40// LIBC_ENABLE_CONSTEXPR as a flag to control whether the function should be
41// constexpr qualified or not.
42#if LIBC_ENABLE_CONSTEXPR && \
43 (LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED || \
44 (defined(LIBC_COMPILER_IS_GCC) && (LIBC_COMPILER_GCC_VER >= 900)) || \
45 (defined(LIBC_COMPILER_IS_CLANG) && LIBC_COMPILER_CLANG_VER >= 1100))
46#define LIBC_HAS_CONSTANT_EVALUATION
47#define LIBC_CONSTEXPR constexpr
48#else
49#define LIBC_CONSTEXPR
50#endif
51
52// Uses the platform specific specialization
53#define LIBC_THREAD_MODE_PLATFORM 0
54
55// Mutex guards nothing, used in single-threaded implementations
56#define LIBC_THREAD_MODE_SINGLE 1
57
58// Vendor provides implementation
59#define LIBC_THREAD_MODE_EXTERNAL 2
60
61// libcxx doesn't define LIBC_THREAD_MODE, unless that is passed in the command
62// line in the CMake invocation. This defaults to the original implementation
63// (before changes in https://github.com/llvm/llvm-project/pull/145358)
64#ifndef LIBC_THREAD_MODE
65#define LIBC_THREAD_MODE LIBC_THREAD_MODE_PLATFORM
66#endif // LIBC_THREAD_MODE
67
68#if LIBC_THREAD_MODE != LIBC_THREAD_MODE_PLATFORM && \
69 LIBC_THREAD_MODE != LIBC_THREAD_MODE_SINGLE && \
70 LIBC_THREAD_MODE != LIBC_THREAD_MODE_EXTERNAL
71#error LIBC_THREAD_MODE must be one of the following values: \
72LIBC_THREAD_MODE_PLATFORM, \
73LIBC_THREAD_MODE_SINGLE, \
74LIBC_THREAD_MODE_EXTERNAL.
75#endif
76
77#if LIBC_THREAD_MODE == LIBC_THREAD_MODE_SINGLE
78#define LIBC_THREAD_LOCAL
79#else
80#define LIBC_THREAD_LOCAL thread_local
81#endif
82
83#if __cplusplus >= 202002L
84#define LIBC_CONSTINIT constinit
85#elif __has_attribute(__require_constant_initialization__)
86#define LIBC_CONSTINIT __attribute__((__require_constant_initialization__))
87#else
88#define LIBC_CONSTINIT
89#endif
90
91#if defined(__clang__) && __has_attribute(preferred_type)
92#define LIBC_PREFERED_TYPE(TYPE) [[clang::preferred_type(TYPE)]]
93#else
94#define LIBC_PREFERED_TYPE(TYPE)
95#endif
96
97#if __has_attribute(ext_vector_type) && \
98 LIBC_HAS_FEATURE(ext_vector_type_boolean)
99#define LIBC_HAS_VECTOR_TYPE 1
100#else
101#define LIBC_HAS_VECTOR_TYPE 0
102#endif
103
104#if __has_attribute(no_sanitize)
105// Disable regular and hardware-supported ASan for functions that may
106// intentionally make out-of-bounds access. Disable TSan as well, as it detects
107// out-of-bounds accesses to heap memory.
108#define LIBC_NO_SANITIZE_OOB_ACCESS \
109 __attribute__((no_sanitize("address", "hwaddress", "thread")))
110#else
111#define LIBC_NO_SANITIZE_OOB_ACCESS
112#endif
113
114#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_ATTRIBUTES_H
115