| 6 | #define BASE_COMPILER_SPECIFIC_H_ |
| 7 |
| 8 | #include "build/build_config.h" |
| 9 |
| 10 | #if defined(COMPILER_MSVC) && !defined(__clang__) |
| 11 | #error "Only clang-cl is supported on Windows, see https://crbug.com/988071" |
| 12 | #endif |
| 13 |
| 14 | // Annotate a variable indicating it's ok if the variable is not used. |
| 15 | // (Typically used to silence a compiler warning when the assignment |
| 16 | // is important for some other reason.) |
| 17 | // Use like: |
| 18 | // int x = ...; |
| 19 | // ALLOW_UNUSED_LOCAL(x); |
| 20 | #define ALLOW_UNUSED_LOCAL(x) (void)x |
| 21 |
| 22 | // Annotate a typedef or function indicating it's ok if it's not used. |
| 23 | // Use like: |
| 24 | // typedef Foo Bar ALLOW_UNUSED_TYPE; |
| 25 | #if defined(COMPILER_GCC) || defined(__clang__) |
| 26 | #define ALLOW_UNUSED_TYPE __attribute__((unused)) |
| 27 | #else |
| 28 | #define ALLOW_UNUSED_TYPE |
| 29 | #endif |
| 30 |
| 31 | // Annotate a function indicating it should not be inlined. |
| 32 | // Use like: |
| 33 | // NOINLINE void DoStuff() { ... } |
| 34 | #if defined(COMPILER_GCC) |
| 35 | #define NOINLINE __attribute__((noinline)) |
| 36 | #elif defined(COMPILER_MSVC) |
| 37 | #define NOINLINE __declspec(noinline) |
| 38 | #else |
| 39 | #define NOINLINE |
| 40 | #endif |
| 41 |
| 42 | #if defined(COMPILER_GCC) && defined(NDEBUG) |
| 43 | #define ALWAYS_INLINE inline __attribute__((__always_inline__)) |
| 44 | #elif defined(COMPILER_MSVC) && defined(NDEBUG) |
| 45 | #define ALWAYS_INLINE __forceinline |
| 46 | #else |
| 47 | #define ALWAYS_INLINE inline |
| 48 | #endif |
| 49 |
| 50 | // Specify memory alignment for structs, classes, etc. |
| 51 | // Use like: |
| 52 | // class ALIGNAS(16) MyClass { ... } |
| 53 | // ALIGNAS(16) int array[4]; |
| 54 | // |
| 55 | // In most places you can use the C++11 keyword "alignas", which is preferred. |
| 56 | // |
| 57 | // But compilers have trouble mixing __attribute__((...)) syntax with |
| 58 | // alignas(...) syntax. |
| 59 | // |
| 60 | // Doesn't work in clang or gcc: |
| 61 | // struct alignas(16) __attribute__((packed)) S { char c; }; |
| 62 | // Works in clang but not gcc: |
| 63 | // struct __attribute__((packed)) alignas(16) S2 { char c; }; |
| 64 | // Works in clang and gcc: |
| 65 | // struct alignas(16) S3 { char c; } __attribute__((packed)); |
| 66 | // |
| 67 | // There are also some attributes that must be specified *before* a class |
| 68 | // definition: visibility (used for exporting functions/classes) is one of |
| 69 | // these attributes. This means that it is not possible to use alignas() with a |
| 70 | // class that is marked as exported. |
| 71 | #if defined(COMPILER_MSVC) |
| 72 | #define ALIGNAS(byte_alignment) __declspec(align(byte_alignment)) |
| 73 | #elif defined(COMPILER_GCC) |
| 74 | #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment))) |
| 75 | #endif |
| 76 |
| 77 | // Annotate a function indicating the caller must examine the return value. |
| 78 | // Use like: |
| 79 | // int foo() WARN_UNUSED_RESULT; |
| 80 | // To explicitly ignore a result, see |ignore_result()| in base/macros.h. |
| 81 | #undef WARN_UNUSED_RESULT |
| 82 | #if defined(COMPILER_GCC) || defined(__clang__) |
| 83 | #define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
| 84 | #else |
| 85 | #define WARN_UNUSED_RESULT |
| 86 | #endif |
| 87 |
| 88 | // Tell the compiler a function is using a printf-style format string. |
| 89 | // |format_param| is the one-based index of the format string parameter; |
| 90 | // |dots_param| is the one-based index of the "..." parameter. |
| 91 | // For v*printf functions (which take a va_list), pass 0 for dots_param. |
| 92 | // (This is undocumented but matches what the system C headers do.) |
| 93 | // For member functions, the implicit this parameter counts as index 1. |
| 94 | #if defined(COMPILER_GCC) || defined(__clang__) |
| 95 | #define PRINTF_FORMAT(format_param, dots_param) \ |
| 96 | __attribute__((format(printf, format_param, dots_param))) |
| 97 | #else |
| 98 | #define PRINTF_FORMAT(format_param, dots_param) |
| 99 | #endif |
| 100 |
| 101 | // WPRINTF_FORMAT is the same, but for wide format strings. |
| 102 | // This doesn't appear to yet be implemented in any compiler. |
| 103 | // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 . |
| 104 | #define WPRINTF_FORMAT(format_param, dots_param) |
| 105 | // If available, it would look like: |
| 106 | // __attribute__((format(wprintf, format_param, dots_param))) |
| 107 |
| 108 | // Sanitizers annotations. |
| 109 | #if defined(__has_attribute) |
| 110 | #if __has_attribute(no_sanitize) |
| 111 | #define NO_SANITIZE(what) __attribute__((no_sanitize(what))) |
| 112 | #endif |
| 113 | #endif |
| 114 | #if !defined(NO_SANITIZE) |
| 115 | #define NO_SANITIZE(what) |
| 116 | #endif |
| 117 |
| 118 | // MemorySanitizer annotations. |
| 119 | #if defined(MEMORY_SANITIZER) && !defined(OS_NACL) |
| 120 | #include <sanitizer/msan_interface.h> |
| 121 |
| 122 | // Mark a memory region fully initialized. |
| 123 | // Use this to annotate code that deliberately reads uninitialized data, for |
| 124 | // example a GC scavenging root set pointers from the stack. |
| 125 | #define MSAN_UNPOISON(p, size) __msan_unpoison(p, size) |
| 126 |
| 127 | // Check a memory region for initializedness, as if it was being used here. |
| 128 | // If any bits are uninitialized, crash with an MSan report. |
| 129 | // Use this to sanitize data which MSan won't be able to track, e.g. before |
| 130 | // passing data to another process via shared memory. |
| 131 | #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) \ |
| 132 | __msan_check_mem_is_initialized(p, size) |
| 133 | #else // MEMORY_SANITIZER |
| 134 | #define MSAN_UNPOISON(p, size) |
| 135 | #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) |
| 136 | #endif // MEMORY_SANITIZER |
| 137 |
| 138 | // DISABLE_CFI_PERF -- Disable Control Flow Integrity for perf reasons. |
| 139 | #if !defined(DISABLE_CFI_PERF) |
| 140 | #if defined(__clang__) && defined(OFFICIAL_BUILD) |
| 141 | #define DISABLE_CFI_PERF __attribute__((no_sanitize("cfi"))) |
| 142 | #else |
| 143 | #define DISABLE_CFI_PERF |
| 144 | #endif |
| 145 | #endif |
| 146 |
| 147 | // Macro useful for writing cross-platform function pointers. |
| 148 | #if !defined(CDECL) |
| 149 | #if defined(OS_WIN) |
| 150 | #define CDECL __cdecl |
| 151 | #else // defined(OS_WIN) |
| 152 | #define CDECL |
| 153 | #endif // defined(OS_WIN) |
| 154 | #endif // !defined(CDECL) |
| 155 |
| 156 | // Macro for hinting that an expression is likely to be false. |
| 157 | #if !defined(UNLIKELY) |
| 158 | #if defined(COMPILER_GCC) || defined(__clang__) |
| 159 | #define UNLIKELY(x) __builtin_expect(!!(x), 0) |
| 160 | #else |
| 161 | #define UNLIKELY(x) (x) |
| 162 | #endif // defined(COMPILER_GCC) |
| 163 | #endif // !defined(UNLIKELY) |
| 164 |
| 165 | #if !defined(LIKELY) |
| 166 | #if defined(COMPILER_GCC) || defined(__clang__) |
| 167 | #define LIKELY(x) __builtin_expect(!!(x), 1) |
| 168 | #else |
| 169 | #define LIKELY(x) (x) |
| 170 | #endif // defined(COMPILER_GCC) |
| 171 | #endif // !defined(LIKELY) |
| 172 |
| 173 | // Compiler feature-detection. |
| 174 | // clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension |
| 175 | #if defined(__has_feature) |
| 176 | #define HAS_FEATURE(FEATURE) __has_feature(FEATURE) |
| 177 | #else |
| 178 | #define HAS_FEATURE(FEATURE) 0 |
| 179 | #endif |
| 180 |
| 181 | // Macro for telling -Wimplicit-fallthrough that a fallthrough is intentional. |
| 182 | #if defined(__clang__) |
| 183 | #define FALLTHROUGH [[clang::fallthrough]] |
| 184 | #else |
| 185 | #define FALLTHROUGH |
| 186 | #endif |
| 187 |
| 188 | #if defined(COMPILER_GCC) |
| 189 | #define PRETTY_FUNCTION __PRETTY_FUNCTION__ |
| 190 | #elif defined(COMPILER_MSVC) |
| 191 | #define PRETTY_FUNCTION __FUNCSIG__ |
| 192 | #else |
| 193 | // See https://en.cppreference.com/w/c/language/function_definition#func |
| 194 | #define PRETTY_FUNCTION __func__ |
| 195 | #endif |
| 196 |
| 197 | #if !defined(CPU_ARM_NEON) |
| 198 | #if defined(__arm__) |
| 199 | #if !defined(__ARMEB__) && !defined(__ARM_EABI__) && !defined(__EABI__) && \ |
| 200 | !defined(__VFP_FP__) && !defined(_WIN32_WCE) && !defined(ANDROID) |
| 201 | #error Chromium does not support middle endian architecture |
| 202 | #endif |
| 203 | #if defined(__ARM_NEON__) |
| 204 | #define CPU_ARM_NEON 1 |
| 205 | #endif |
| 206 | #endif // defined(__arm__) |
| 207 | #endif // !defined(CPU_ARM_NEON) |
| 208 |
| 209 | #if !defined(HAVE_MIPS_MSA_INTRINSICS) |
| 210 | #if defined(__mips_msa) && defined(__mips_isa_rev) && (__mips_isa_rev >= 5) |
| 211 | #define HAVE_MIPS_MSA_INTRINSICS 1 |
| 212 | #endif |
| 213 | #endif |
| 214 |
| 215 | #if defined(__clang__) && __has_attribute(uninitialized) |
| 216 | // Attribute "uninitialized" disables -ftrivial-auto-var-init=pattern for |
| 217 | // the specified variable. |
| 218 | // Library-wide alternative is |
| 219 | // 'configs -= [ "//build/config/compiler:default_init_stack_vars" ]' in .gn |
| 220 | // file. |
| 221 | // |
| 222 | // See "init_stack_vars" in build/config/compiler/BUILD.gn and |
| 223 | // http://crbug.com/977230 |
| 224 | // "init_stack_vars" is enabled for non-official builds and we hope to enable it |
| 225 | // in official build in 2020 as well. The flag writes fixed pattern into |
| 226 | // uninitialized parts of all local variables. In rare cases such initialization |
| 227 | // is undesirable and attribute can be used: |
| 228 | // 1. Degraded performance |
| 229 | // In most cases compiler is able to remove additional stores. E.g. if memory is |
| 230 | // never accessed or properly initialized later. Preserved stores mostly will |
| 231 | // not affect program performance. However if compiler failed on some |
| 232 | // performance critical code we can get a visible regression in a benchmark. |
| 233 | // 2. memset, memcpy calls |
| 234 | // Compiler may replaces some memory writes with memset or memcpy calls. This is |
| 235 | // not -ftrivial-auto-var-init specific, but it can happen more likely with the |
| 236 | // flag. It can be a problem if code is not linked with C run-time library. |
| 237 | // |
| 238 | // Note: The flag is security risk mitigation feature. So in future the |
| 239 | // attribute uses should be avoided when possible. However to enable this |
| 240 | // mitigation on the most of the code we need to be less strict now and minimize |
| 241 | // number of exceptions later. So if in doubt feel free to use attribute, but |
| 242 | // please document the problem for someone who is going to cleanup it later. |
| 243 | // E.g. platform, bot, benchmark or test name in patch description or next to |
| 244 | // the attribute. |
| 245 | #define STACK_UNINITIALIZED __attribute__((uninitialized)) |
| 246 | #else |
| 247 | #define STACK_UNINITIALIZED |
| 248 | #endif |
| 249 |
| 250 | #endif // BASE_COMPILER_SPECIFIC_H_ |
| 251 |