folly/folly/CPortability.h

/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

/* These definitions are in a separate file so that they
 * may be included from C- as well as C++-based projects. */

#include <folly/portability/Config.h>

/**
 * Portable version check.
 */
#ifndef __GNUC_PREREQ
#if defined __GNUC__ && defined __GNUC_MINOR__
/* nolint */
#define __GNUC_PREREQ(maj, min)
#else
/* nolint */
#define __GNUC_PREREQ
#endif
#endif

// portable version check for clang
#ifndef __CLANG_PREREQ
#if defined __clang__ && defined __clang_major__ && defined __clang_minor__
/* nolint */
#define __CLANG_PREREQ(maj, min)
#else
/* nolint */
#define __CLANG_PREREQ
#endif
#endif

#if defined(__has_builtin)
#define FOLLY_HAS_BUILTIN(...)
#else
#define FOLLY_HAS_BUILTIN
#endif

#if defined(__has_feature)
#define FOLLY_HAS_FEATURE(...)
#else
#define FOLLY_HAS_FEATURE
#endif

/* FOLLY_SANITIZE_ADDRESS is defined to 1 if the current compilation unit
 * is being compiled with ASAN or HWASAN enabled.
 *
 * Beware when using this macro in a header file: this macro may change values
 * across compilation units if some libraries are built with ASAN/HWASAN enabled
 * and some built with ASAN/HWSAN disabled. For instance, this may occur, if
 * folly itself was compiled without ASAN/HWSAN but a downstream project that
 * uses folly is compiling with ASAN/HWSAN enabled.
 *
 * Use FOLLY_LIBRARY_SANITIZE_ADDRESS (defined in folly-config.h) to check if
 * folly itself was compiled with ASAN enabled.
 */
#ifndef FOLLY_SANITIZE_ADDRESS
#if FOLLY_HAS_FEATURE(address_sanitizer) || defined(__SANITIZE_ADDRESS__) || \
    FOLLY_HAS_FEATURE(hwaddress_sanitizer)
#define FOLLY_SANITIZE_ADDRESS
#endif
#endif

/* Define attribute wrapper for function attribute used to disable
 * address sanitizer instrumentation. Unfortunately, this attribute
 * has issues when inlining is used, so disable that as well. */
#ifdef FOLLY_SANITIZE_ADDRESS
#if defined(__clang__)
#if __has_attribute(__no_sanitize__)
#define FOLLY_DISABLE_ADDRESS_SANITIZER
#elif __has_attribute(__no_address_safety_analysis__)
#define FOLLY_DISABLE_ADDRESS_SANITIZER
#elif __has_attribute(__no_sanitize_address__)
#define FOLLY_DISABLE_ADDRESS_SANITIZER
#endif
#elif defined(__GNUC__)
#define FOLLY_DISABLE_ADDRESS_SANITIZER
#elif defined(_MSC_VER)
#define FOLLY_DISABLE_ADDRESS_SANITIZER
#endif
#endif
#ifndef FOLLY_DISABLE_ADDRESS_SANITIZER
#define FOLLY_DISABLE_ADDRESS_SANITIZER
#endif

/* Define a convenience macro to test when thread sanitizer is being used
 * across the different compilers (e.g. clang, gcc) */
#ifndef FOLLY_SANITIZE_THREAD
#if FOLLY_HAS_FEATURE(thread_sanitizer) || defined(__SANITIZE_THREAD__)
#define FOLLY_SANITIZE_THREAD
#endif
#endif

#ifdef FOLLY_SANITIZE_THREAD
#define FOLLY_DISABLE_THREAD_SANITIZER
#else
#define FOLLY_DISABLE_THREAD_SANITIZER
#endif

/**
 * Define a convenience macro to test when memory sanitizer is being used
 * across the different compilers (e.g. clang, gcc)
 */
#ifndef FOLLY_SANITIZE_MEMORY
#if FOLLY_HAS_FEATURE(memory_sanitizer) || defined(__SANITIZE_MEMORY__)
#define FOLLY_SANITIZE_MEMORY
#endif
#endif

#ifdef FOLLY_SANITIZE_MEMORY
#define FOLLY_DISABLE_MEMORY_SANITIZER
#else
#define FOLLY_DISABLE_MEMORY_SANITIZER
#endif

/**
 * Define a convenience macro to test when dataflow sanitizer is being used
 * across the different compilers (e.g. clang, gcc)
 */
#ifndef FOLLY_SANITIZE_DATAFLOW
#if FOLLY_HAS_FEATURE(dataflow_sanitizer) || defined(__SANITIZE_DATAFLOW__)
#define FOLLY_SANITIZE_DATAFLOW
#endif
#endif

#ifdef FOLLY_SANITIZE_DATAFLOW
#define FOLLY_DISABLE_DATAFLOW_SANITIZER
#else
#define FOLLY_DISABLE_DATAFLOW_SANITIZER
#endif

/**
 * Define a convenience macro to test when undefined-behavior sanitizer is being
 * used across the different compilers (e.g. clang, gcc)
 */
#ifndef FOLLY_SANITIZE_UNDEFINED_BEHAVIOR
#if FOLLY_HAS_FEATURE(undefined_behavior_sanitizer) || \
    defined(__SANITIZER_UNDEFINED__)
#define FOLLY_SANITIZE_UNDEFINED_BEHAVIOR
#endif
#endif

#ifdef FOLLY_SANITIZE_UNDEFINED_BEHAVIOR
#define FOLLY_DISABLE_UNDEFINED_BEHAVIOR_SANITIZER
#else
#define FOLLY_DISABLE_UNDEFINED_BEHAVIOR_SANITIZER(...)
#endif

/**
 * Define a convenience macro to test when ASAN, UBSAN, TSAN or MSAN sanitizer
 * are being used
 */
#ifndef FOLLY_SANITIZE
#if defined(FOLLY_SANITIZE_ADDRESS) || defined(FOLLY_SANITIZE_THREAD) ||  \
    defined(FOLLY_SANITIZE_MEMORY) || defined(FOLLY_SANITIZE_DATAFLOW) || \
    defined(FOLLY_SANITIZE_UNDEFINED_BEHAVIOR)
#define FOLLY_SANITIZE
#endif
#endif

#define FOLLY_DISABLE_SANITIZERS

/**
 * Macro for marking functions as having public visibility.
 */
#if defined(__GNUC__)
#define FOLLY_EXPORT
#else
#define FOLLY_EXPORT
#endif

// noinline
#ifdef _MSC_VER
#define FOLLY_NOINLINE
#elif defined(__HIP_PLATFORM_HCC__)
// HIP software stack defines its own __noinline__ macro.
#define FOLLY_NOINLINE
#elif defined(__GNUC__)
#define FOLLY_NOINLINE
#else
#define FOLLY_NOINLINE
#endif

// always inline
#ifdef _MSC_VER
#define FOLLY_ALWAYS_INLINE
#elif defined(__GNUC__)
#define FOLLY_ALWAYS_INLINE
#else
#define FOLLY_ALWAYS_INLINE
#endif

// attribute hidden
#if defined(_MSC_VER)
#define FOLLY_ATTR_VISIBILITY_HIDDEN
#elif defined(__GNUC__)
#define FOLLY_ATTR_VISIBILITY_HIDDEN
#else
#define FOLLY_ATTR_VISIBILITY_HIDDEN
#endif

// An attribute for marking symbols as weak, if supported
#if FOLLY_HAVE_WEAK_SYMBOLS
#define FOLLY_ATTR_WEAK
#else
#define FOLLY_ATTR_WEAK
#endif

// Microsoft ABI version (can be overridden manually if necessary)
#ifndef FOLLY_MICROSOFT_ABI_VER
#ifdef _MSC_VER
#define FOLLY_MICROSOFT_ABI_VER
#endif
#endif

//  FOLLY_ERASE
//
//  A conceptual attribute/syntax combo for erasing a function from the build
//  artifacts and forcing all call-sites to inline the callee, at least as far
//  as each compiler supports.
//
//  Semantically includes the inline specifier.
#define FOLLY_ERASE

//  FOLLY_ERASE_NOINLINE
//
//  Like FOLLY_ERASE, but also noinline. The naming similarity with FOLLY_ERASE
//  is specifically desirable.
#define FOLLY_ERASE_NOINLINE

//  FOLLY_ERASE_HACK_GCC
//
//  Equivalent to FOLLY_ERASE, but without hiding under gcc. Useful when applied
//  to a function which may sometimes be hidden separately, for example by being
//  declared in an anonymous namespace, since in such cases with -Wattributes
//  enabled, gcc would emit: 'visibility' attribute ignored.
//
//  Semantically includes the inline specifier.
#if defined(__GNUC__) && !defined(__clang__)
#define FOLLY_ERASE_HACK_GCC
#else
#define FOLLY_ERASE_HACK_GCC
#endif

//  FOLLY_ERASE_TRYCATCH
//
//  Equivalent to FOLLY_ERASE, but for code which might contain explicit
//  exception handling. Has the effect of FOLLY_ERASE, except under MSVC which
//  warns about __forceinline when functions contain exception handling.
//
//  Semantically includes the inline specifier.
#ifdef _MSC_VER
#define FOLLY_ERASE_TRYCATCH
#else
#define FOLLY_ERASE_TRYCATCH
#endif

// Generalize warning push/pop.
#if defined(__GNUC__) || defined(__clang__)
// Clang & GCC
#define FOLLY_PUSH_WARNING
#define FOLLY_POP_WARNING
#define FOLLY_GNU_DISABLE_WARNING_INTERNAL2(warningName)
#define FOLLY_GNU_DISABLE_WARNING(warningName)
#ifdef __clang__
#define FOLLY_CLANG_DISABLE_WARNING(warningName)
#define FOLLY_GCC_DISABLE_WARNING(warningName)
#else
#define FOLLY_CLANG_DISABLE_WARNING
#define FOLLY_GCC_DISABLE_WARNING
#endif
#define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
#elif defined(_MSC_VER)
#define FOLLY_PUSH_WARNING
#define FOLLY_POP_WARNING
// Disable the GCC warnings.
#define FOLLY_GNU_DISABLE_WARNING
#define FOLLY_GCC_DISABLE_WARNING
#define FOLLY_CLANG_DISABLE_WARNING
#define FOLLY_MSVC_DISABLE_WARNING
#else
#define FOLLY_PUSH_WARNING
#define FOLLY_POP_WARNING
#define FOLLY_GNU_DISABLE_WARNING
#define FOLLY_GCC_DISABLE_WARNING
#define FOLLY_CLANG_DISABLE_WARNING
#define FOLLY_MSVC_DISABLE_WARNING
#endif

#ifdef FOLLY_HAVE_SHADOW_LOCAL_WARNINGS
#define FOLLY_GCC_DISABLE_NEW_SHADOW_WARNINGS
#else
#define FOLLY_GCC_DISABLE_NEW_SHADOW_WARNINGS
#endif

#if defined(_MSC_VER)
#define FOLLY_MSVC_DECLSPEC
#else
#define FOLLY_MSVC_DECLSPEC(...)
#endif