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// Uses the platform specific specialization
33#define LIBC_THREAD_MODE_PLATFORM 0
34
35// Mutex guards nothing, used in single-threaded implementations
36#define LIBC_THREAD_MODE_SINGLE 1
37
38// Vendor provides implementation
39#define LIBC_THREAD_MODE_EXTERNAL 2
40
41// libcxx doesn't define LIBC_THREAD_MODE, unless that is passed in the command
42// line in the CMake invocation. This defaults to the original implementation
43// (before changes in https://github.com/llvm/llvm-project/pull/145358)
44#ifndef LIBC_THREAD_MODE
45#define LIBC_THREAD_MODE LIBC_THREAD_MODE_PLATFORM
46#endif // LIBC_THREAD_MODE
47
48#if LIBC_THREAD_MODE != LIBC_THREAD_MODE_PLATFORM && \
49 LIBC_THREAD_MODE != LIBC_THREAD_MODE_SINGLE && \
50 LIBC_THREAD_MODE != LIBC_THREAD_MODE_EXTERNAL
51#error LIBC_THREAD_MODE must be one of the following values: \
52LIBC_THREAD_MODE_PLATFORM, \
53LIBC_THREAD_MODE_SINGLE, \
54LIBC_THREAD_MODE_EXTERNAL.
55#endif
56
57#if LIBC_THREAD_MODE == LIBC_THREAD_MODE_SINGLE
58#define LIBC_THREAD_LOCAL
59#else
60#define LIBC_THREAD_LOCAL thread_local
61#endif
62
63#if __cplusplus >= 202002L
64#define LIBC_CONSTINIT constinit
65#elif __has_attribute(__require_constant_initialization__)
66#define LIBC_CONSTINIT __attribute__((__require_constant_initialization__))
67#else
68#define LIBC_CONSTINIT
69#endif
70
71#if defined(__clang__) && __has_attribute(preferred_type)
72#define LIBC_PREFERED_TYPE(TYPE) [[clang::preferred_type(TYPE)]]
73#else
74#define LIBC_PREFERED_TYPE(TYPE)
75#endif
76
77#if __has_attribute(ext_vector_type) && \
78 LIBC_HAS_FEATURE(ext_vector_type_boolean)
79#define LIBC_HAS_VECTOR_TYPE 1
80#else
81#define LIBC_HAS_VECTOR_TYPE 0
82#endif
83
84#if __has_attribute(no_sanitize)
85// Disable regular and hardware-supported ASan for functions that may
86// intentionally make out-of-bounds access. Disable TSan as well, as it detects
87// out-of-bounds accesses to heap memory.
88#define LIBC_NO_SANITIZE_OOB_ACCESS \
89 __attribute__((no_sanitize("address", "hwaddress", "thread")))
90#else
91#define LIBC_NO_SANITIZE_OOB_ACCESS
92#endif
93
94#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_ATTRIBUTES_H
95