xref: /llvm-project/libc/src/__support/common.h (revision 4c91662a519705c2d0b6e002214a3e307f09b373)
1d9bbad27SSiva Chandra Reddy //===-- Common internal contructs -------------------------------*- C++ -*-===//
2aefeb5f1SSiva Chandra Reddy //
3aefeb5f1SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4aefeb5f1SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
5aefeb5f1SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6aefeb5f1SSiva Chandra Reddy //
7aefeb5f1SSiva Chandra Reddy //===----------------------------------------------------------------------===//
8aefeb5f1SSiva Chandra Reddy 
9270547f3SGuillaume Chatelet #ifndef LLVM_LIBC_SRC___SUPPORT_COMMON_H
10270547f3SGuillaume Chatelet #define LLVM_LIBC_SRC___SUPPORT_COMMON_H
11aefeb5f1SSiva Chandra Reddy 
129c4e0056SGuillaume Chatelet #ifndef LIBC_NAMESPACE
139c4e0056SGuillaume Chatelet #error "LIBC_NAMESPACE macro is not defined."
149c4e0056SGuillaume Chatelet #endif
159c4e0056SGuillaume Chatelet 
1629f8e076SGuillaume Chatelet #include "src/__support/macros/attributes.h"
175ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
18f100ec25SGuillaume Chatelet #include "src/__support/macros/properties/architectures.h"
19aefeb5f1SSiva Chandra Reddy 
20aefeb5f1SSiva Chandra Reddy #ifndef LLVM_LIBC_FUNCTION_ATTR
21aefeb5f1SSiva Chandra Reddy #define LLVM_LIBC_FUNCTION_ATTR
22aefeb5f1SSiva Chandra Reddy #endif
23aefeb5f1SSiva Chandra Reddy 
24*4c91662aSCaslyn Tonelli // clang-format off
2514667119Slntue // Allow each function `func` to have extra attributes specified by defining:
2614667119Slntue // `LLVM_LIBC_FUNCTION_ATTR_func` macro, which should always start with
2714667119Slntue // "LLVM_LIBC_EMPTY, "
2814667119Slntue //
2914667119Slntue // For examples:
3014667119Slntue // #define LLVM_LIBC_FUNCTION_ATTR_memcpy LLVM_LIBC_EMPTY, [[gnu::weak]]
31*4c91662aSCaslyn Tonelli // #define LLVM_LIBC_FUNCTION_ATTR_memchr LLVM_LIBC_EMPTY, [[gnu::weak]] [[gnu::visibility("default")]]
32*4c91662aSCaslyn Tonelli // clang-format on
3314667119Slntue #define LLVM_LIBC_EMPTY
3414667119Slntue 
3514667119Slntue #define GET_SECOND(first, second, ...) second
3614667119Slntue #define EXPAND_THEN_SECOND(name) GET_SECOND(name, LLVM_LIBC_EMPTY)
3714667119Slntue 
3814667119Slntue #define LLVM_LIBC_ATTR(name) EXPAND_THEN_SECOND(LLVM_LIBC_FUNCTION_ATTR_##name)
3914667119Slntue 
400f031daeSTue Ly // MacOS needs to be excluded because it does not support aliasing.
41e74281a3SJoseph Huber #if defined(LIBC_COPT_PUBLIC_PACKAGING) && (!defined(__APPLE__))
42d30de984SRoland McGrath #define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist)                           \
4314667119Slntue   LLVM_LIBC_ATTR(name)                                                         \
44b6bc9d72SGuillaume Chatelet   LLVM_LIBC_FUNCTION_ATTR decltype(LIBC_NAMESPACE::name)                       \
45aefeb5f1SSiva Chandra Reddy       __##name##_impl__ __asm__(#name);                                        \
46d86a6eacSlntue   decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]];                   \
47aefeb5f1SSiva Chandra Reddy   type __##name##_impl__ arglist
48aefeb5f1SSiva Chandra Reddy #else
49d30de984SRoland McGrath #define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) type name arglist
50aefeb5f1SSiva Chandra Reddy #endif
51aefeb5f1SSiva Chandra Reddy 
52d30de984SRoland McGrath // This extra layer of macro allows `name` to be a macro to rename a function.
53d30de984SRoland McGrath #define LLVM_LIBC_FUNCTION(type, name, arglist)                                \
54d30de984SRoland McGrath   LLVM_LIBC_FUNCTION_IMPL(type, name, arglist)
55d30de984SRoland McGrath 
565ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
57a10300a2SGuillaume Chatelet namespace internal {
58019a477cSRoland McGrath LIBC_INLINE constexpr bool same_string(char const *lhs, char const *rhs) {
59a10300a2SGuillaume Chatelet   for (; *lhs || *rhs; ++lhs, ++rhs)
60a10300a2SGuillaume Chatelet     if (*lhs != *rhs)
61a10300a2SGuillaume Chatelet       return false;
62a10300a2SGuillaume Chatelet   return true;
63a10300a2SGuillaume Chatelet }
64a10300a2SGuillaume Chatelet } // namespace internal
655ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
66a10300a2SGuillaume Chatelet 
67f2c9fe45SSiva Chandra #define __LIBC_MACRO_TO_STRING(str) #str
68f2c9fe45SSiva Chandra #define LIBC_MACRO_TO_STRING(str) __LIBC_MACRO_TO_STRING(str)
69f2c9fe45SSiva Chandra 
70a10300a2SGuillaume Chatelet // LLVM_LIBC_IS_DEFINED checks whether a particular macro is defined.
71a10300a2SGuillaume Chatelet // Usage: constexpr bool kUseAvx = LLVM_LIBC_IS_DEFINED(__AVX__);
72a10300a2SGuillaume Chatelet //
73a10300a2SGuillaume Chatelet // This works by comparing the stringified version of the macro with and without
74a10300a2SGuillaume Chatelet // evaluation. If FOO is not undefined both stringifications yield "FOO". If FOO
75a10300a2SGuillaume Chatelet // is defined, one stringification yields "FOO" while the other yields its
76a10300a2SGuillaume Chatelet // stringified value "1".
77a10300a2SGuillaume Chatelet #define LLVM_LIBC_IS_DEFINED(macro)                                            \
78b6bc9d72SGuillaume Chatelet   !LIBC_NAMESPACE::internal::same_string(                                      \
79a10300a2SGuillaume Chatelet       LLVM_LIBC_IS_DEFINED__EVAL_AND_STRINGIZE(macro), #macro)
80a10300a2SGuillaume Chatelet #define LLVM_LIBC_IS_DEFINED__EVAL_AND_STRINGIZE(s) #s
81a10300a2SGuillaume Chatelet 
82270547f3SGuillaume Chatelet #endif // LLVM_LIBC_SRC___SUPPORT_COMMON_H
83