94 lines · c
1//===-- Common internal contructs -------------------------------*- 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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC___SUPPORT_COMMON_H10#define LLVM_LIBC_SRC___SUPPORT_COMMON_H11 12#ifndef LIBC_NAMESPACE13#error "LIBC_NAMESPACE macro is not defined."14#endif15 16#include "src/__support/macros/attributes.h"17#include "src/__support/macros/config.h"18#include "src/__support/macros/properties/architectures.h"19#include "src/__support/macros/properties/compiler.h"20 21#ifndef LLVM_LIBC_FUNCTION_ATTR22#define LLVM_LIBC_FUNCTION_ATTR23#endif24 25// clang-format off26// Allow each function `func` to have extra attributes specified by defining:27// `LLVM_LIBC_FUNCTION_ATTR_func` macro, which should always start with28// "LLVM_LIBC_EMPTY, "29//30// For examples:31// #define LLVM_LIBC_FUNCTION_ATTR_memcpy LLVM_LIBC_EMPTY, [[gnu::weak]]32// #define LLVM_LIBC_FUNCTION_ATTR_memchr LLVM_LIBC_EMPTY, [[gnu::weak]] [[gnu::visibility("default")]]33// clang-format on34#define LLVM_LIBC_EMPTY35 36#define GET_SECOND(first, second, ...) second37#define EXPAND_THEN_SECOND(name) GET_SECOND(name, LLVM_LIBC_EMPTY)38 39#define LLVM_LIBC_ATTR(name) EXPAND_THEN_SECOND(LLVM_LIBC_FUNCTION_ATTR_##name)40 41// At the moment, [[gnu::alias()]] is not supported on MacOS, and it is needed42// to cleanly export and alias the C++ symbol `LIBC_NAMESPACE::func` with the C43// symbol `func`. So for public packaging on MacOS, we will only export the C44// symbol. Moreover, a C symbol `func` in macOS is mangled as `_func`.45#if defined(LIBC_COPT_PUBLIC_PACKAGING) && !defined(LIBC_COMPILER_IS_MSVC)46#ifndef __APPLE__47#define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) \48 LLVM_LIBC_ATTR(name) \49 LLVM_LIBC_FUNCTION_ATTR decltype(LIBC_NAMESPACE::name) \50 __##name##_impl__ asm(#name); \51 decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]]; \52 type __##name##_impl__ arglist53#else // __APPLE__54#define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) \55 LLVM_LIBC_ATTR(name) \56 LLVM_LIBC_FUNCTION_ATTR decltype(LIBC_NAMESPACE::name) name asm("_" #name); \57 type name arglist58#endif // __APPLE__59#else // LIBC_COPT_PUBLIC_PACKAGING60#define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) type name arglist61#endif // LIBC_COPT_PUBLIC_PACKAGING62 63// This extra layer of macro allows `name` to be a macro to rename a function.64#define LLVM_LIBC_FUNCTION(type, name, arglist) \65 LLVM_LIBC_FUNCTION_IMPL(type, name, arglist)66 67namespace LIBC_NAMESPACE_DECL {68namespace internal {69LIBC_INLINE constexpr bool same_string(char const *lhs, char const *rhs) {70 for (; *lhs || *rhs; ++lhs, ++rhs)71 if (*lhs != *rhs)72 return false;73 return true;74}75} // namespace internal76} // namespace LIBC_NAMESPACE_DECL77 78#define __LIBC_MACRO_TO_STRING(str) #str79#define LIBC_MACRO_TO_STRING(str) __LIBC_MACRO_TO_STRING(str)80 81// LLVM_LIBC_IS_DEFINED checks whether a particular macro is defined.82// Usage: constexpr bool kUseAvx = LLVM_LIBC_IS_DEFINED(__AVX__);83//84// This works by comparing the stringified version of the macro with and without85// evaluation. If FOO is not undefined both stringifications yield "FOO". If FOO86// is defined, one stringification yields "FOO" while the other yields its87// stringified value "1".88#define LLVM_LIBC_IS_DEFINED(macro) \89 !LIBC_NAMESPACE::internal::same_string( \90 LLVM_LIBC_IS_DEFINED__EVAL_AND_STRINGIZE(macro), #macro)91#define LLVM_LIBC_IS_DEFINED__EVAL_AND_STRINGIZE(s) #s92 93#endif // LLVM_LIBC_SRC___SUPPORT_COMMON_H94