| // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #ifndef THIRD_PARTY_BASE_COMPILER_SPECIFIC_H_ | |
| #define THIRD_PARTY_BASE_COMPILER_SPECIFIC_H_ | |
| #include "build/build_config.h" | |
| // A wrapper around `__has_attribute`, similar to HAS_CPP_ATTRIBUTE. | |
| #if defined(__has_attribute) | |
| #define HAS_ATTRIBUTE(x) __has_attribute(x) | |
| #else | |
| #define HAS_ATTRIBUTE(x) 0 | |
| #endif | |
| // Annotate a function indicating it should not be inlined. | |
| // Use like: | |
| // NOINLINE void DoStuff() { ... } | |
| #if defined(COMPILER_GCC) | |
| #define NOINLINE __attribute__((noinline)) | |
| #elif defined(COMPILER_MSVC) | |
| #define NOINLINE __declspec(noinline) | |
| #else | |
| #define NOINLINE | |
| #endif | |
| #if defined(COMPILER_GCC) && defined(NDEBUG) | |
| #define ALWAYS_INLINE inline __attribute__((__always_inline__)) | |
| #elif defined(COMPILER_MSVC) && defined(NDEBUG) | |
| #define ALWAYS_INLINE __forceinline | |
| #else | |
| #define ALWAYS_INLINE inline | |
| #endif | |
| // Specify memory alignment for structs, classes, etc. | |
| // Use like: | |
| // class ALIGNAS(16) MyClass { ... } | |
| // ALIGNAS(16) int array[4]; | |
| // | |
| // In most places you can use the C++11 keyword "alignas", which is preferred. | |
| // | |
| // But compilers have trouble mixing __attribute__((...)) syntax with | |
| // alignas(...) syntax. | |
| // | |
| // Doesn't work in clang or gcc: | |
| // struct alignas(16) __attribute__((packed)) S { char c; }; | |
| // Works in clang but not gcc: | |
| // struct __attribute__((packed)) alignas(16) S2 { char c; }; | |
| // Works in clang and gcc: | |
| // struct alignas(16) S3 { char c; } __attribute__((packed)); | |
| // | |
| // There are also some attributes that must be specified *before* a class | |
| // definition: visibility (used for exporting functions/classes) is one of | |
| // these attributes. This means that it is not possible to use alignas() with a | |
| // class that is marked as exported. | |
| #if defined(COMPILER_MSVC) | |
| #define ALIGNAS(byte_alignment) __declspec(align(byte_alignment)) | |
| #elif defined(COMPILER_GCC) | |
| #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment))) | |
| #endif | |
| // Tell the compiler a function is using a printf-style format string. | |
| // |format_param| is the one-based index of the format string parameter; | |
| // |dots_param| is the one-based index of the "..." parameter. | |
| // For v*printf functions (which take a va_list), pass 0 for dots_param. | |
| // (This is undocumented but matches what the system C headers do.) | |
| #if defined(COMPILER_GCC) || defined(__clang__) | |
| #define PRINTF_FORMAT(format_param, dots_param) \ | |
| __attribute__((format(printf, format_param, dots_param))) | |
| #else | |
| #define PRINTF_FORMAT(format_param, dots_param) | |
| #endif | |
| // WPRINTF_FORMAT is the same, but for wide format strings. | |
| // This doesn't appear to yet be implemented in any compiler. | |
| // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 . | |
| #define WPRINTF_FORMAT(format_param, dots_param) | |
| // If available, it would look like: | |
| // __attribute__((format(wprintf, format_param, dots_param))) | |
| // Sanitizers annotations. | |
| #if defined(__has_attribute) | |
| #if __has_attribute(no_sanitize) | |
| #define NO_SANITIZE(what) __attribute__((no_sanitize(what))) | |
| #endif | |
| #endif | |
| #if !defined(NO_SANITIZE) | |
| #define NO_SANITIZE(what) | |
| #endif | |
| // MemorySanitizer annotations. | |
| #if defined(MEMORY_SANITIZER) && !BUILDFLAG(IS_NACL) | |
| #include <sanitizer/msan_interface.h> | |
| // Mark a memory region fully initialized. | |
| // Use this to annotate code that deliberately reads uninitialized data, for | |
| // example a GC scavenging root set pointers from the stack. | |
| #define MSAN_UNPOISON(p, size) __msan_unpoison(p, size) | |
| // Check a memory region for initializedness, as if it was being used here. | |
| // If any bits are uninitialized, crash with an MSan report. | |
| // Use this to sanitize data which MSan won't be able to track, e.g. before | |
| // passing data to another process via shared memory. | |
| #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) \ | |
| __msan_check_mem_is_initialized(p, size) | |
| #else // MEMORY_SANITIZER | |
| #define MSAN_UNPOISON(p, size) | |
| #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) | |
| #endif // MEMORY_SANITIZER | |
| // DISABLE_CFI_PERF -- Disable Control Flow Integrity for perf reasons. | |
| #if !defined(DISABLE_CFI_PERF) | |
| #if defined(__clang__) && defined(OFFICIAL_BUILD) | |
| #define DISABLE_CFI_PERF __attribute__((no_sanitize("cfi"))) | |
| #else | |
| #define DISABLE_CFI_PERF | |
| #endif | |
| #endif | |
| // Macro useful for writing cross-platform function pointers. | |
| #if !defined(CDECL) | |
| #if BUILDFLAG(IS_WIN) | |
| #define CDECL __cdecl | |
| #else // BUILDFLAG(IS_WIN) | |
| #define CDECL | |
| #endif // BUILDFLAG(IS_WIN) | |
| #endif // !defined(CDECL) | |
| // Macro for hinting that an expression is likely to be false. | |
| #if !defined(UNLIKELY) | |
| #if defined(COMPILER_GCC) || defined(__clang__) | |
| #define UNLIKELY(x) __builtin_expect(!!(x), 0) | |
| #else | |
| #define UNLIKELY(x) (x) | |
| #endif // defined(COMPILER_GCC) | |
| #endif // !defined(UNLIKELY) | |
| #if !defined(LIKELY) | |
| #if defined(COMPILER_GCC) || defined(__clang__) | |
| #define LIKELY(x) __builtin_expect(!!(x), 1) | |
| #else | |
| #define LIKELY(x) (x) | |
| #endif // defined(COMPILER_GCC) | |
| #endif // !defined(LIKELY) | |
| // Compiler feature-detection. | |
| // clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension | |
| #if defined(__has_feature) | |
| #define HAS_FEATURE(FEATURE) __has_feature(FEATURE) | |
| #else | |
| #define HAS_FEATURE(FEATURE) 0 | |
| #endif | |
| // Marks a type as being eligible for the "trivial" ABI despite having a | |
| // non-trivial destructor or copy/move constructor. Such types can be relocated | |
| // after construction by simply copying their memory, which makes them eligible | |
| // to be passed in registers. The canonical example is std::unique_ptr. | |
| // | |
| // Use with caution; this has some subtle effects on constructor/destructor | |
| // ordering and will be very incorrect if the type relies on its address | |
| // remaining constant. When used as a function argument (by value), the value | |
| // may be constructed in the caller's stack frame, passed in a register, and | |
| // then used and destructed in the callee's stack frame. A similar thing can | |
| // occur when values are returned. | |
| // | |
| // TRIVIAL_ABI is not needed for types which have a trivial destructor and | |
| // copy/move constructors, such as base::TimeTicks and other POD. | |
| // | |
| // It is also not likely to be effective on types too large to be passed in one | |
| // or two registers on typical target ABIs. | |
| // | |
| // See also: | |
| // https://clang.llvm.org/docs/AttributeReference.html#trivial-abi | |
| // https://libcxx.llvm.org/docs/DesignDocs/UniquePtrTrivialAbi.html | |
| #if defined(__clang__) && HAS_ATTRIBUTE(trivial_abi) | |
| #define TRIVIAL_ABI [[clang::trivial_abi]] | |
| #else | |
| #define TRIVIAL_ABI | |
| #endif | |
| #if defined(__clang__) | |
| #define GSL_OWNER [[gsl::Owner]] | |
| #define GSL_POINTER [[gsl::Pointer]] | |
| #else | |
| #define GSL_OWNER | |
| #define GSL_POINTER | |
| #endif | |
| #endif // THIRD_PARTY_BASE_COMPILER_SPECIFIC_H_ |