[email protected] | 2149cc62 | 2012-02-14 01:12:12 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 4 | |
| 5 | #ifndef BASE_COMPILER_SPECIFIC_H_ |
| 6 | #define BASE_COMPILER_SPECIFIC_H_ |
| 7 | |
| 8 | #include "build/build_config.h" |
| 9 | |
| 10 | #if defined(COMPILER_MSVC) |
| 11 | |
Nico Weber | 5979181 | 2019-07-27 04:02:11 | [diff] [blame] | 12 | #if !defined(__clang__) |
| 13 | #error "Only clang-cl is supported on Windows, see https://crbug.com/988071" |
| 14 | #endif |
| 15 | |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 16 | // Macros for suppressing and disabling warnings on MSVC. |
| 17 | // |
| 18 | // Warning numbers are enumerated at: |
| 19 | // http://msdn.microsoft.com/en-us/library/8x5x43k7(VS.80).aspx |
| 20 | // |
| 21 | // The warning pragma: |
| 22 | // http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx |
| 23 | // |
| 24 | // Using __pragma instead of #pragma inside macros: |
| 25 | // http://msdn.microsoft.com/en-us/library/d9x1s805.aspx |
| 26 | |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 27 | // MSVC_PUSH_DISABLE_WARNING pushes |n| onto a stack of warnings to be disabled. |
| 28 | // The warning remains disabled until popped by MSVC_POP_WARNING. |
| 29 | #define MSVC_PUSH_DISABLE_WARNING(n) __pragma(warning(push)) \ |
| 30 | __pragma(warning(disable:n)) |
[email protected] | b91902d | 2008-09-16 19:28:06 | [diff] [blame] | 31 | |
[email protected] | b91902d | 2008-09-16 19:28:06 | [diff] [blame] | 32 | // Pop effects of innermost MSVC_PUSH_* macro. |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 33 | #define MSVC_POP_WARNING() __pragma(warning(pop)) |
| 34 | |
[email protected] | dd9afc0b | 2008-11-21 23:58:09 | [diff] [blame] | 35 | #else // Not MSVC |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 36 | |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 37 | #define MSVC_PUSH_DISABLE_WARNING(n) |
| 38 | #define MSVC_POP_WARNING() |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 39 | #define MSVC_DISABLE_OPTIMIZE() |
| 40 | #define MSVC_ENABLE_OPTIMIZE() |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 41 | |
| 42 | #endif // COMPILER_MSVC |
| 43 | |
Bruce Dawson | f141f57 | 2019-01-10 19:06:36 | [diff] [blame] | 44 | // These macros can be helpful when investigating compiler bugs or when |
| 45 | // investigating issues in local optimized builds, by temporarily disabling |
| 46 | // optimizations for a single function or file. These macros should never be |
| 47 | // used to permanently work around compiler bugs or other mysteries, and should |
| 48 | // not be used in landed changes. |
| 49 | #if !defined(OFFICIAL_BUILD) |
| 50 | #if defined(__clang__) |
| 51 | #define DISABLE_OPTIMIZE() __pragma(clang optimize off) |
| 52 | #define ENABLE_OPTIMIZE() __pragma(clang optimize on) |
| 53 | #elif defined(COMPILER_MSVC) |
| 54 | #define DISABLE_OPTIMIZE() __pragma(optimize("", off)) |
| 55 | #define ENABLE_OPTIMIZE() __pragma(optimize("", on)) |
| 56 | #else |
| 57 | // These macros are not currently available for other compiler options. |
| 58 | #endif |
| 59 | // These macros are not available in official builds. |
| 60 | #endif // !defined(OFFICIAL_BUILD) |
| 61 | |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 62 | // Annotate a variable indicating it's ok if the variable is not used. |
| 63 | // (Typically used to silence a compiler warning when the assignment |
| 64 | // is important for some other reason.) |
| 65 | // Use like: |
pkasting | 99867ef | 2014-10-16 23:49:24 | [diff] [blame] | 66 | // int x = ...; |
| 67 | // ALLOW_UNUSED_LOCAL(x); |
kmarshall | d10bb7f7 | 2017-04-26 02:55:41 | [diff] [blame] | 68 | #define ALLOW_UNUSED_LOCAL(x) (void)x |
pkasting | 99867ef | 2014-10-16 23:49:24 | [diff] [blame] | 69 | |
| 70 | // Annotate a typedef or function indicating it's ok if it's not used. |
| 71 | // Use like: |
| 72 | // typedef Foo Bar ALLOW_UNUSED_TYPE; |
thakis | 803f983 | 2015-07-20 18:10:55 | [diff] [blame] | 73 | #if defined(COMPILER_GCC) || defined(__clang__) |
pkasting | 99867ef | 2014-10-16 23:49:24 | [diff] [blame] | 74 | #define ALLOW_UNUSED_TYPE __attribute__((unused)) |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 75 | #else |
pkasting | 99867ef | 2014-10-16 23:49:24 | [diff] [blame] | 76 | #define ALLOW_UNUSED_TYPE |
[email protected] | 2149cc62 | 2012-02-14 01:12:12 | [diff] [blame] | 77 | #endif |
| 78 | |
| 79 | // Annotate a function indicating it should not be inlined. |
| 80 | // Use like: |
| 81 | // NOINLINE void DoStuff() { ... } |
| 82 | #if defined(COMPILER_GCC) |
| 83 | #define NOINLINE __attribute__((noinline)) |
| 84 | #elif defined(COMPILER_MSVC) |
| 85 | #define NOINLINE __declspec(noinline) |
| 86 | #else |
[email protected] | 50795a0 | 2011-05-09 20:11:01 | [diff] [blame] | 87 | #define NOINLINE |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 88 | #endif |
| 89 | |
Ivan Krasin | 9c098a0d | 2018-08-05 03:57:57 | [diff] [blame] | 90 | #if defined(COMPILER_GCC) && defined(NDEBUG) |
palmer | 58184a828 | 2016-11-08 19:15:39 | [diff] [blame] | 91 | #define ALWAYS_INLINE inline __attribute__((__always_inline__)) |
Ivan Krasin | 9c098a0d | 2018-08-05 03:57:57 | [diff] [blame] | 92 | #elif defined(COMPILER_MSVC) && defined(NDEBUG) |
palmer | 58184a828 | 2016-11-08 19:15:39 | [diff] [blame] | 93 | #define ALWAYS_INLINE __forceinline |
| 94 | #else |
| 95 | #define ALWAYS_INLINE inline |
| 96 | #endif |
| 97 | |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 98 | // Specify memory alignment for structs, classes, etc. |
| 99 | // Use like: |
| 100 | // class ALIGNAS(16) MyClass { ... } |
| 101 | // ALIGNAS(16) int array[4]; |
brettw | 16289b3e | 2017-06-13 21:58:40 | [diff] [blame] | 102 | // |
| 103 | // In most places you can use the C++11 keyword "alignas", which is preferred. |
| 104 | // |
| 105 | // But compilers have trouble mixing __attribute__((...)) syntax with |
| 106 | // alignas(...) syntax. |
| 107 | // |
| 108 | // Doesn't work in clang or gcc: |
| 109 | // struct alignas(16) __attribute__((packed)) S { char c; }; |
| 110 | // Works in clang but not gcc: |
| 111 | // struct __attribute__((packed)) alignas(16) S2 { char c; }; |
| 112 | // Works in clang and gcc: |
| 113 | // struct alignas(16) S3 { char c; } __attribute__((packed)); |
| 114 | // |
| 115 | // There are also some attributes that must be specified *before* a class |
| 116 | // definition: visibility (used for exporting functions/classes) is one of |
| 117 | // these attributes. This means that it is not possible to use alignas() with a |
| 118 | // class that is marked as exported. |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 119 | #if defined(COMPILER_MSVC) |
| 120 | #define ALIGNAS(byte_alignment) __declspec(align(byte_alignment)) |
| 121 | #elif defined(COMPILER_GCC) |
| 122 | #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment))) |
| 123 | #endif |
| 124 | |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 125 | // Annotate a function indicating the caller must examine the return value. |
| 126 | // Use like: |
| 127 | // int foo() WARN_UNUSED_RESULT; |
toyoshim | 20986cf | 2015-07-03 08:38:07 | [diff] [blame] | 128 | // To explicitly ignore a result, see |ignore_result()| in base/macros.h. |
dcheng | 7764fe9 | 2015-10-10 01:17:26 | [diff] [blame] | 129 | #undef WARN_UNUSED_RESULT |
| 130 | #if defined(COMPILER_GCC) || defined(__clang__) |
[email protected] | dd9afc0b | 2008-11-21 23:58:09 | [diff] [blame] | 131 | #define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 132 | #else |
| 133 | #define WARN_UNUSED_RESULT |
| 134 | #endif |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 135 | |
| 136 | // Tell the compiler a function is using a printf-style format string. |
| 137 | // |format_param| is the one-based index of the format string parameter; |
| 138 | // |dots_param| is the one-based index of the "..." parameter. |
| 139 | // For v*printf functions (which take a va_list), pass 0 for dots_param. |
| 140 | // (This is undocumented but matches what the system C headers do.) |
Nico Weber | fc7c8dd | 2019-02-28 21:28:44 | [diff] [blame] | 141 | // For member functions, the implicit this parameter counts as index 1. |
Bruce Dawson | 73528d1 | 2017-08-09 01:04:29 | [diff] [blame] | 142 | #if defined(COMPILER_GCC) || defined(__clang__) |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 143 | #define PRINTF_FORMAT(format_param, dots_param) \ |
| 144 | __attribute__((format(printf, format_param, dots_param))) |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 145 | #else |
| 146 | #define PRINTF_FORMAT(format_param, dots_param) |
| 147 | #endif |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 148 | |
| 149 | // WPRINTF_FORMAT is the same, but for wide format strings. |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 150 | // This doesn't appear to yet be implemented in any compiler. |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 151 | // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 . |
| 152 | #define WPRINTF_FORMAT(format_param, dots_param) |
| 153 | // If available, it would look like: |
| 154 | // __attribute__((format(wprintf, format_param, dots_param))) |
| 155 | |
etienneb | 4e9250a | 2016-11-18 18:47:53 | [diff] [blame] | 156 | // Sanitizers annotations. |
| 157 | #if defined(__has_attribute) |
| 158 | #if __has_attribute(no_sanitize) |
| 159 | #define NO_SANITIZE(what) __attribute__((no_sanitize(what))) |
| 160 | #endif |
| 161 | #endif |
| 162 | #if !defined(NO_SANITIZE) |
| 163 | #define NO_SANITIZE(what) |
| 164 | #endif |
| 165 | |
[email protected] | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 166 | // MemorySanitizer annotations. |
[email protected] | 36c2b137 | 2014-02-07 18:54:44 | [diff] [blame] | 167 | #if defined(MEMORY_SANITIZER) && !defined(OS_NACL) |
[email protected] | eb82dfb | 2014-02-03 19:51:17 | [diff] [blame] | 168 | #include <sanitizer/msan_interface.h> |
[email protected] | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 169 | |
| 170 | // Mark a memory region fully initialized. |
| 171 | // Use this to annotate code that deliberately reads uninitialized data, for |
| 172 | // example a GC scavenging root set pointers from the stack. |
thestig | 1a42b407 | 2015-03-16 22:36:55 | [diff] [blame] | 173 | #define MSAN_UNPOISON(p, size) __msan_unpoison(p, size) |
| 174 | |
| 175 | // Check a memory region for initializedness, as if it was being used here. |
| 176 | // If any bits are uninitialized, crash with an MSan report. |
| 177 | // Use this to sanitize data which MSan won't be able to track, e.g. before |
| 178 | // passing data to another process via shared memory. |
| 179 | #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) \ |
| 180 | __msan_check_mem_is_initialized(p, size) |
[email protected] | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 181 | #else // MEMORY_SANITIZER |
thestig | 1a42b407 | 2015-03-16 22:36:55 | [diff] [blame] | 182 | #define MSAN_UNPOISON(p, size) |
| 183 | #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) |
[email protected] | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 184 | #endif // MEMORY_SANITIZER |
| 185 | |
krasin | 825ce48 | 2016-08-27 11:01:11 | [diff] [blame] | 186 | // DISABLE_CFI_PERF -- Disable Control Flow Integrity for perf reasons. |
| 187 | #if !defined(DISABLE_CFI_PERF) |
krasin | 40f7c78 | 2016-09-22 19:04:27 | [diff] [blame] | 188 | #if defined(__clang__) && defined(OFFICIAL_BUILD) |
krasin | 825ce48 | 2016-08-27 11:01:11 | [diff] [blame] | 189 | #define DISABLE_CFI_PERF __attribute__((no_sanitize("cfi"))) |
| 190 | #else |
| 191 | #define DISABLE_CFI_PERF |
| 192 | #endif |
| 193 | #endif |
| 194 | |
[email protected] | 5a8d4ce | 2013-12-18 17:42:27 | [diff] [blame] | 195 | // Macro useful for writing cross-platform function pointers. |
| 196 | #if !defined(CDECL) |
| 197 | #if defined(OS_WIN) |
| 198 | #define CDECL __cdecl |
| 199 | #else // defined(OS_WIN) |
| 200 | #define CDECL |
| 201 | #endif // defined(OS_WIN) |
| 202 | #endif // !defined(CDECL) |
| 203 | |
[email protected] | 2bc0c699 | 2014-02-13 16:11:04 | [diff] [blame] | 204 | // Macro for hinting that an expression is likely to be false. |
| 205 | #if !defined(UNLIKELY) |
Vladimir Levin | 6b77771 | 2017-09-09 00:12:05 | [diff] [blame] | 206 | #if defined(COMPILER_GCC) || defined(__clang__) |
[email protected] | 2bc0c699 | 2014-02-13 16:11:04 | [diff] [blame] | 207 | #define UNLIKELY(x) __builtin_expect(!!(x), 0) |
| 208 | #else |
| 209 | #define UNLIKELY(x) (x) |
| 210 | #endif // defined(COMPILER_GCC) |
| 211 | #endif // !defined(UNLIKELY) |
| 212 | |
palmer | 58184a828 | 2016-11-08 19:15:39 | [diff] [blame] | 213 | #if !defined(LIKELY) |
Vladimir Levin | 6b77771 | 2017-09-09 00:12:05 | [diff] [blame] | 214 | #if defined(COMPILER_GCC) || defined(__clang__) |
Chris Palmer | ad4cb83f | 2016-11-18 20:02:25 | [diff] [blame] | 215 | #define LIKELY(x) __builtin_expect(!!(x), 1) |
palmer | 58184a828 | 2016-11-08 19:15:39 | [diff] [blame] | 216 | #else |
| 217 | #define LIKELY(x) (x) |
| 218 | #endif // defined(COMPILER_GCC) |
| 219 | #endif // !defined(LIKELY) |
| 220 | |
jfb | d81c1ce | 2016-04-05 20:50:35 | [diff] [blame] | 221 | // Compiler feature-detection. |
jfb | a8dc9dd8 | 2016-04-06 20:20:31 | [diff] [blame] | 222 | // clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension |
| 223 | #if defined(__has_feature) |
| 224 | #define HAS_FEATURE(FEATURE) __has_feature(FEATURE) |
| 225 | #else |
| 226 | #define HAS_FEATURE(FEATURE) 0 |
jfb | d81c1ce | 2016-04-05 20:50:35 | [diff] [blame] | 227 | #endif |
| 228 | |
Nico Weber | 0ae8836 | 2018-01-25 19:26:02 | [diff] [blame] | 229 | // Macro for telling -Wimplicit-fallthrough that a fallthrough is intentional. |
Nico Weber | 0ae8836 | 2018-01-25 19:26:02 | [diff] [blame] | 230 | #if defined(__clang__) |
| 231 | #define FALLTHROUGH [[clang::fallthrough]] |
| 232 | #else |
| 233 | #define FALLTHROUGH |
| 234 | #endif |
Nico Weber | 0ae8836 | 2018-01-25 19:26:02 | [diff] [blame] | 235 | |
Alex Clarke | 23c6cf7 | 2018-11-21 13:22:27 | [diff] [blame] | 236 | #if defined(COMPILER_GCC) |
| 237 | #define PRETTY_FUNCTION __PRETTY_FUNCTION__ |
| 238 | #elif defined(COMPILER_MSVC) |
| 239 | #define PRETTY_FUNCTION __FUNCSIG__ |
| 240 | #else |
| 241 | // See https://en.cppreference.com/w/c/language/function_definition#func |
| 242 | #define PRETTY_FUNCTION __func__ |
| 243 | #endif |
| 244 | |
Henrique Ferreiro | 6daf71db | 2019-04-03 13:12:42 | [diff] [blame] | 245 | #if !defined(CPU_ARM_NEON) |
| 246 | #if defined(__arm__) |
| 247 | #if !defined(__ARMEB__) && !defined(__ARM_EABI__) && !defined(__EABI__) && \ |
| 248 | !defined(__VFP_FP__) && !defined(_WIN32_WCE) && !defined(ANDROID) |
| 249 | #error Chromium does not support middle endian architecture |
| 250 | #endif |
| 251 | #if defined(__ARM_NEON__) |
| 252 | #define CPU_ARM_NEON 1 |
| 253 | #endif |
| 254 | #endif // defined(__arm__) |
| 255 | #endif // !defined(CPU_ARM_NEON) |
| 256 | |
| 257 | #if !defined(HAVE_MIPS_MSA_INTRINSICS) |
| 258 | #if defined(__mips_msa) && defined(__mips_isa_rev) && (__mips_isa_rev >= 5) |
| 259 | #define HAVE_MIPS_MSA_INTRINSICS 1 |
| 260 | #endif |
| 261 | #endif |
| 262 | |
[email protected] | dd9afc0b | 2008-11-21 23:58:09 | [diff] [blame] | 263 | #endif // BASE_COMPILER_SPECIFIC_H_ |