[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 | |
Nico Weber | fb053cc | 2020-03-03 13:33:05 | [diff] [blame] | 10 | #if defined(COMPILER_MSVC) && !defined(__clang__) |
Nico Weber | 5979181 | 2019-07-27 04:02:11 | [diff] [blame] | 11 | #error "Only clang-cl is supported on Windows, see https://crbug.com/988071" |
| 12 | #endif |
| 13 | |
Jan Wilken Dörrie | f8d479d | 2020-11-23 12:21:13 | [diff] [blame] | 14 | // This is a wrapper around `__has_cpp_attribute`, which can be used to test for |
| 15 | // the presence of an attribute. In case the compiler does not support this |
| 16 | // macro it will simply evaluate to 0. |
| 17 | // |
| 18 | // References: |
| 19 | // https://wg21.link/sd6#testing-for-the-presence-of-an-attribute-__has_cpp_attribute |
| 20 | // https://wg21.link/cpp.cond#:__has_cpp_attribute |
| 21 | #if defined(__has_cpp_attribute) |
| 22 | #define HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) |
| 23 | #else |
| 24 | #define HAS_CPP_ATTRIBUTE(x) 0 |
| 25 | #endif |
| 26 | |
Jann Horn | 9e4b4855 | 2021-03-04 14:34:27 | [diff] [blame] | 27 | // A wrapper around `__has_builtin`, similar to HAS_CPP_ATTRIBUTE. |
| 28 | #if defined(__has_builtin) |
| 29 | #define HAS_BUILTIN(x) __has_builtin(x) |
| 30 | #else |
| 31 | #define HAS_BUILTIN(x) 0 |
| 32 | #endif |
| 33 | |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 34 | // Annotate a variable indicating it's ok if the variable is not used. |
| 35 | // (Typically used to silence a compiler warning when the assignment |
| 36 | // is important for some other reason.) |
| 37 | // Use like: |
pkasting | 99867ef | 2014-10-16 23:49:24 | [diff] [blame] | 38 | // int x = ...; |
| 39 | // ALLOW_UNUSED_LOCAL(x); |
kmarshall | d10bb7f7 | 2017-04-26 02:55:41 | [diff] [blame] | 40 | #define ALLOW_UNUSED_LOCAL(x) (void)x |
pkasting | 99867ef | 2014-10-16 23:49:24 | [diff] [blame] | 41 | |
| 42 | // Annotate a typedef or function indicating it's ok if it's not used. |
| 43 | // Use like: |
| 44 | // typedef Foo Bar ALLOW_UNUSED_TYPE; |
thakis | 803f983 | 2015-07-20 18:10:55 | [diff] [blame] | 45 | #if defined(COMPILER_GCC) || defined(__clang__) |
pkasting | 99867ef | 2014-10-16 23:49:24 | [diff] [blame] | 46 | #define ALLOW_UNUSED_TYPE __attribute__((unused)) |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 47 | #else |
pkasting | 99867ef | 2014-10-16 23:49:24 | [diff] [blame] | 48 | #define ALLOW_UNUSED_TYPE |
[email protected] | 2149cc62 | 2012-02-14 01:12:12 | [diff] [blame] | 49 | #endif |
| 50 | |
| 51 | // Annotate a function indicating it should not be inlined. |
| 52 | // Use like: |
| 53 | // NOINLINE void DoStuff() { ... } |
| 54 | #if defined(COMPILER_GCC) |
| 55 | #define NOINLINE __attribute__((noinline)) |
| 56 | #elif defined(COMPILER_MSVC) |
| 57 | #define NOINLINE __declspec(noinline) |
| 58 | #else |
[email protected] | 50795a0 | 2011-05-09 20:11:01 | [diff] [blame] | 59 | #define NOINLINE |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 60 | #endif |
| 61 | |
Ivan Krasin | 9c098a0d | 2018-08-05 03:57:57 | [diff] [blame] | 62 | #if defined(COMPILER_GCC) && defined(NDEBUG) |
palmer | 58184a828 | 2016-11-08 19:15:39 | [diff] [blame] | 63 | #define ALWAYS_INLINE inline __attribute__((__always_inline__)) |
Ivan Krasin | 9c098a0d | 2018-08-05 03:57:57 | [diff] [blame] | 64 | #elif defined(COMPILER_MSVC) && defined(NDEBUG) |
palmer | 58184a828 | 2016-11-08 19:15:39 | [diff] [blame] | 65 | #define ALWAYS_INLINE __forceinline |
| 66 | #else |
| 67 | #define ALWAYS_INLINE inline |
| 68 | #endif |
| 69 | |
Olivier Li | 19d8925 | 2020-05-13 17:57:55 | [diff] [blame] | 70 | // Annotate a function indicating it should never be tail called. Useful to make |
| 71 | // sure callers of the annotated function are never omitted from call-stacks. |
| 72 | // To provide the complementary behavior (prevent the annotated function from |
| 73 | // being omitted) look at NOINLINE. Also note that this doesn't prevent code |
| 74 | // folding of multiple identical caller functions into a single signature. To |
Bruce Dawson | 7915efd | 2021-01-27 18:07:58 | [diff] [blame] | 75 | // prevent code folding, see NO_CODE_FOLDING() in base/debug/alias.h. |
Olivier Li | 19d8925 | 2020-05-13 17:57:55 | [diff] [blame] | 76 | // Use like: |
| 77 | // void NOT_TAIL_CALLED FooBar(); |
| 78 | #if defined(__clang__) && __has_attribute(not_tail_called) |
| 79 | #define NOT_TAIL_CALLED __attribute__((not_tail_called)) |
| 80 | #else |
| 81 | #define NOT_TAIL_CALLED |
| 82 | #endif |
| 83 | |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 84 | // Specify memory alignment for structs, classes, etc. |
| 85 | // Use like: |
| 86 | // class ALIGNAS(16) MyClass { ... } |
| 87 | // ALIGNAS(16) int array[4]; |
brettw | 16289b3e | 2017-06-13 21:58:40 | [diff] [blame] | 88 | // |
| 89 | // In most places you can use the C++11 keyword "alignas", which is preferred. |
| 90 | // |
| 91 | // But compilers have trouble mixing __attribute__((...)) syntax with |
| 92 | // alignas(...) syntax. |
| 93 | // |
| 94 | // Doesn't work in clang or gcc: |
| 95 | // struct alignas(16) __attribute__((packed)) S { char c; }; |
| 96 | // Works in clang but not gcc: |
| 97 | // struct __attribute__((packed)) alignas(16) S2 { char c; }; |
| 98 | // Works in clang and gcc: |
| 99 | // struct alignas(16) S3 { char c; } __attribute__((packed)); |
| 100 | // |
| 101 | // There are also some attributes that must be specified *before* a class |
| 102 | // definition: visibility (used for exporting functions/classes) is one of |
| 103 | // these attributes. This means that it is not possible to use alignas() with a |
| 104 | // class that is marked as exported. |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 105 | #if defined(COMPILER_MSVC) |
| 106 | #define ALIGNAS(byte_alignment) __declspec(align(byte_alignment)) |
| 107 | #elif defined(COMPILER_GCC) |
| 108 | #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment))) |
| 109 | #endif |
| 110 | |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 111 | // Annotate a function indicating the caller must examine the return value. |
| 112 | // Use like: |
| 113 | // int foo() WARN_UNUSED_RESULT; |
toyoshim | 20986cf | 2015-07-03 08:38:07 | [diff] [blame] | 114 | // To explicitly ignore a result, see |ignore_result()| in base/macros.h. |
dcheng | 7764fe9 | 2015-10-10 01:17:26 | [diff] [blame] | 115 | #undef WARN_UNUSED_RESULT |
| 116 | #if defined(COMPILER_GCC) || defined(__clang__) |
[email protected] | dd9afc0b | 2008-11-21 23:58:09 | [diff] [blame] | 117 | #define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 118 | #else |
| 119 | #define WARN_UNUSED_RESULT |
| 120 | #endif |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 121 | |
Jan Wilken Dörrie | f8d479d | 2020-11-23 12:21:13 | [diff] [blame] | 122 | // In case the compiler supports it NO_UNIQUE_ADDRESS evaluates to the C++20 |
| 123 | // attribute [[no_unique_address]]. This allows annotating data members so that |
| 124 | // they need not have an address distinct from all other non-static data members |
| 125 | // of its class. |
| 126 | // |
| 127 | // References: |
| 128 | // * https://en.cppreference.com/w/cpp/language/attributes/no_unique_address |
| 129 | // * https://wg21.link/dcl.attr.nouniqueaddr |
| 130 | #if HAS_CPP_ATTRIBUTE(no_unique_address) |
| 131 | #define NO_UNIQUE_ADDRESS [[no_unique_address]] |
| 132 | #else |
| 133 | #define NO_UNIQUE_ADDRESS |
| 134 | #endif |
| 135 | |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 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) \ |
Vitaly Buka | 2b79076 | 2019-12-20 21:11:48 | [diff] [blame] | 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. |
Vitaly Buka | 2b79076 | 2019-12-20 21:11:48 | [diff] [blame] | 173 | #define MSAN_UNPOISON(p, size) __msan_unpoison(p, size) |
thestig | 1a42b407 | 2015-03-16 22:36:55 | [diff] [blame] | 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) \ |
Vitaly Buka | 2b79076 | 2019-12-20 21:11:48 | [diff] [blame] | 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 | |
Will Harris | 9a033b0 | 2020-07-11 01:26:54 | [diff] [blame] | 195 | // DISABLE_CFI_ICALL -- Disable Control Flow Integrity indirect call checks. |
| 196 | #if !defined(DISABLE_CFI_ICALL) |
| 197 | #if defined(OS_WIN) |
| 198 | // Windows also needs __declspec(guard(nocf)). |
| 199 | #define DISABLE_CFI_ICALL NO_SANITIZE("cfi-icall") __declspec(guard(nocf)) |
| 200 | #else |
| 201 | #define DISABLE_CFI_ICALL NO_SANITIZE("cfi-icall") |
| 202 | #endif |
| 203 | #endif |
| 204 | #if !defined(DISABLE_CFI_ICALL) |
| 205 | #define DISABLE_CFI_ICALL |
| 206 | #endif |
| 207 | |
[email protected] | 5a8d4ce | 2013-12-18 17:42:27 | [diff] [blame] | 208 | // Macro useful for writing cross-platform function pointers. |
| 209 | #if !defined(CDECL) |
| 210 | #if defined(OS_WIN) |
| 211 | #define CDECL __cdecl |
| 212 | #else // defined(OS_WIN) |
| 213 | #define CDECL |
| 214 | #endif // defined(OS_WIN) |
| 215 | #endif // !defined(CDECL) |
| 216 | |
[email protected] | 2bc0c699 | 2014-02-13 16:11:04 | [diff] [blame] | 217 | // Macro for hinting that an expression is likely to be false. |
| 218 | #if !defined(UNLIKELY) |
Vladimir Levin | 6b77771 | 2017-09-09 00:12:05 | [diff] [blame] | 219 | #if defined(COMPILER_GCC) || defined(__clang__) |
[email protected] | 2bc0c699 | 2014-02-13 16:11:04 | [diff] [blame] | 220 | #define UNLIKELY(x) __builtin_expect(!!(x), 0) |
| 221 | #else |
| 222 | #define UNLIKELY(x) (x) |
| 223 | #endif // defined(COMPILER_GCC) |
| 224 | #endif // !defined(UNLIKELY) |
| 225 | |
palmer | 58184a828 | 2016-11-08 19:15:39 | [diff] [blame] | 226 | #if !defined(LIKELY) |
Vladimir Levin | 6b77771 | 2017-09-09 00:12:05 | [diff] [blame] | 227 | #if defined(COMPILER_GCC) || defined(__clang__) |
Chris Palmer | ad4cb83f | 2016-11-18 20:02:25 | [diff] [blame] | 228 | #define LIKELY(x) __builtin_expect(!!(x), 1) |
palmer | 58184a828 | 2016-11-08 19:15:39 | [diff] [blame] | 229 | #else |
| 230 | #define LIKELY(x) (x) |
| 231 | #endif // defined(COMPILER_GCC) |
| 232 | #endif // !defined(LIKELY) |
| 233 | |
jfb | d81c1ce | 2016-04-05 20:50:35 | [diff] [blame] | 234 | // Compiler feature-detection. |
jfb | a8dc9dd8 | 2016-04-06 20:20:31 | [diff] [blame] | 235 | // clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension |
| 236 | #if defined(__has_feature) |
| 237 | #define HAS_FEATURE(FEATURE) __has_feature(FEATURE) |
| 238 | #else |
| 239 | #define HAS_FEATURE(FEATURE) 0 |
jfb | d81c1ce | 2016-04-05 20:50:35 | [diff] [blame] | 240 | #endif |
| 241 | |
Nico Weber | 0ae8836 | 2018-01-25 19:26:02 | [diff] [blame] | 242 | // Macro for telling -Wimplicit-fallthrough that a fallthrough is intentional. |
Nico Weber | 0ae8836 | 2018-01-25 19:26:02 | [diff] [blame] | 243 | #if defined(__clang__) |
| 244 | #define FALLTHROUGH [[clang::fallthrough]] |
| 245 | #else |
| 246 | #define FALLTHROUGH |
| 247 | #endif |
Nico Weber | 0ae8836 | 2018-01-25 19:26:02 | [diff] [blame] | 248 | |
Alex Clarke | 23c6cf7 | 2018-11-21 13:22:27 | [diff] [blame] | 249 | #if defined(COMPILER_GCC) |
| 250 | #define PRETTY_FUNCTION __PRETTY_FUNCTION__ |
| 251 | #elif defined(COMPILER_MSVC) |
| 252 | #define PRETTY_FUNCTION __FUNCSIG__ |
| 253 | #else |
| 254 | // See https://en.cppreference.com/w/c/language/function_definition#func |
| 255 | #define PRETTY_FUNCTION __func__ |
| 256 | #endif |
| 257 | |
Henrique Ferreiro | 6daf71db | 2019-04-03 13:12:42 | [diff] [blame] | 258 | #if !defined(CPU_ARM_NEON) |
| 259 | #if defined(__arm__) |
| 260 | #if !defined(__ARMEB__) && !defined(__ARM_EABI__) && !defined(__EABI__) && \ |
| 261 | !defined(__VFP_FP__) && !defined(_WIN32_WCE) && !defined(ANDROID) |
| 262 | #error Chromium does not support middle endian architecture |
| 263 | #endif |
| 264 | #if defined(__ARM_NEON__) |
| 265 | #define CPU_ARM_NEON 1 |
| 266 | #endif |
| 267 | #endif // defined(__arm__) |
| 268 | #endif // !defined(CPU_ARM_NEON) |
| 269 | |
| 270 | #if !defined(HAVE_MIPS_MSA_INTRINSICS) |
| 271 | #if defined(__mips_msa) && defined(__mips_isa_rev) && (__mips_isa_rev >= 5) |
| 272 | #define HAVE_MIPS_MSA_INTRINSICS 1 |
| 273 | #endif |
| 274 | #endif |
| 275 | |
Vitaly Buka | 2b79076 | 2019-12-20 21:11:48 | [diff] [blame] | 276 | #if defined(__clang__) && __has_attribute(uninitialized) |
| 277 | // Attribute "uninitialized" disables -ftrivial-auto-var-init=pattern for |
| 278 | // the specified variable. |
| 279 | // Library-wide alternative is |
| 280 | // 'configs -= [ "//build/config/compiler:default_init_stack_vars" ]' in .gn |
| 281 | // file. |
| 282 | // |
| 283 | // See "init_stack_vars" in build/config/compiler/BUILD.gn and |
| 284 | // http://crbug.com/977230 |
| 285 | // "init_stack_vars" is enabled for non-official builds and we hope to enable it |
| 286 | // in official build in 2020 as well. The flag writes fixed pattern into |
| 287 | // uninitialized parts of all local variables. In rare cases such initialization |
| 288 | // is undesirable and attribute can be used: |
| 289 | // 1. Degraded performance |
| 290 | // In most cases compiler is able to remove additional stores. E.g. if memory is |
| 291 | // never accessed or properly initialized later. Preserved stores mostly will |
| 292 | // not affect program performance. However if compiler failed on some |
| 293 | // performance critical code we can get a visible regression in a benchmark. |
| 294 | // 2. memset, memcpy calls |
| 295 | // Compiler may replaces some memory writes with memset or memcpy calls. This is |
| 296 | // not -ftrivial-auto-var-init specific, but it can happen more likely with the |
| 297 | // flag. It can be a problem if code is not linked with C run-time library. |
| 298 | // |
| 299 | // Note: The flag is security risk mitigation feature. So in future the |
| 300 | // attribute uses should be avoided when possible. However to enable this |
| 301 | // mitigation on the most of the code we need to be less strict now and minimize |
| 302 | // number of exceptions later. So if in doubt feel free to use attribute, but |
| 303 | // please document the problem for someone who is going to cleanup it later. |
| 304 | // E.g. platform, bot, benchmark or test name in patch description or next to |
| 305 | // the attribute. |
| 306 | #define STACK_UNINITIALIZED __attribute__((uninitialized)) |
| 307 | #else |
| 308 | #define STACK_UNINITIALIZED |
| 309 | #endif |
| 310 | |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 311 | // The ANALYZER_ASSUME_TRUE(bool arg) macro adds compiler-specific hints |
| 312 | // to Clang which control what code paths are statically analyzed, |
| 313 | // and is meant to be used in conjunction with assert & assert-like functions. |
| 314 | // The expression is passed straight through if analysis isn't enabled. |
| 315 | // |
| 316 | // ANALYZER_SKIP_THIS_PATH() suppresses static analysis for the current |
| 317 | // codepath and any other branching codepaths that might follow. |
| 318 | #if defined(__clang_analyzer__) |
| 319 | |
| 320 | inline constexpr bool AnalyzerNoReturn() __attribute__((analyzer_noreturn)) { |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | inline constexpr bool AnalyzerAssumeTrue(bool arg) { |
| 325 | // AnalyzerNoReturn() is invoked and analysis is terminated if |arg| is |
| 326 | // false. |
| 327 | return arg || AnalyzerNoReturn(); |
| 328 | } |
| 329 | |
George Burgess IV | a09d235d | 2020-04-17 13:32:50 | [diff] [blame] | 330 | #define ANALYZER_ASSUME_TRUE(arg) ::AnalyzerAssumeTrue(!!(arg)) |
| 331 | #define ANALYZER_SKIP_THIS_PATH() static_cast<void>(::AnalyzerNoReturn()) |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 332 | #define ANALYZER_ALLOW_UNUSED(var) static_cast<void>(var); |
| 333 | |
| 334 | #else // !defined(__clang_analyzer__) |
| 335 | |
| 336 | #define ANALYZER_ASSUME_TRUE(arg) (arg) |
| 337 | #define ANALYZER_SKIP_THIS_PATH() |
| 338 | #define ANALYZER_ALLOW_UNUSED(var) static_cast<void>(var); |
| 339 | |
| 340 | #endif // defined(__clang_analyzer__) |
| 341 | |
Zequan Wu | 9909f14 | 2021-02-10 03:26:00 | [diff] [blame] | 342 | // Use nomerge attribute to disable optimization of merging multiple same calls. |
Manoj Gupta | db4c885 | 2021-02-26 13:48:04 | [diff] [blame] | 343 | #if defined(__clang__) && __has_attribute(nomerge) |
Zequan Wu | 9909f14 | 2021-02-10 03:26:00 | [diff] [blame] | 344 | #define NOMERGE [[clang::nomerge]] |
| 345 | #else |
| 346 | #define NOMERGE |
| 347 | #endif |
| 348 | |
Jeremy Roman | 810d98d | 2021-04-06 16:46:07 | [diff] [blame] | 349 | // Marks a type as being eligible for the "trivial" ABI despite having a |
| 350 | // non-trivial destructor or copy/move constructor. Such types can be relocated |
| 351 | // after construction by simply copying their memory, which makes them eligible |
| 352 | // to be passed in registers. The canonical example is std::unique_ptr. |
| 353 | // |
| 354 | // Use with caution; this has some subtle effects on constructor/destructor |
| 355 | // ordering and will be very incorrect if the type relies on its address |
| 356 | // remaining constant. When used as a function argument (by value), the value |
| 357 | // may be constructed in the caller's stack frame, passed in a register, and |
| 358 | // then used and destructed in the callee's stack frame. A similar thing can |
| 359 | // occur when values are returned. |
| 360 | // |
| 361 | // TRIVIAL_ABI is not needed for types which have a trivial destructor and |
| 362 | // copy/move constructors, such as base::TimeTicks and other POD. |
| 363 | // |
| 364 | // It is also not likely to be effective on types too large to be passed in one |
| 365 | // or two registers on typical target ABIs. |
| 366 | // |
| 367 | // See also: |
| 368 | // https://clang.llvm.org/docs/AttributeReference.html#trivial-abi |
| 369 | // https://libcxx.llvm.org/docs/DesignDocs/UniquePtrTrivialAbi.html |
| 370 | #if defined(__clang__) && __has_attribute(trivial_abi) |
| 371 | #define TRIVIAL_ABI [[clang::trivial_abi]] |
| 372 | #else |
| 373 | #define TRIVIAL_ABI |
| 374 | #endif |
| 375 | |
Lukasz Anforowicz | 3be38fbb | 2021-04-14 20:29:29 | [diff] [blame^] | 376 | // Marks a member function as reinitializing a moved-from variable. |
| 377 | // See also |
| 378 | // https://clang.llvm.org/extra/clang-tidy/checks/bugprone-use-after-move.html#reinitialization |
| 379 | #if defined(__clang__) && __has_attribute(reinitializes) |
| 380 | #define REINITIALIZES_AFTER_MOVE [[clang::reinitializes]] |
| 381 | #else |
| 382 | #define REINITIALIZES_AFTER_MOVE |
| 383 | #endif |
| 384 | |
[email protected] | dd9afc0b | 2008-11-21 23:58:09 | [diff] [blame] | 385 | #endif // BASE_COMPILER_SPECIFIC_H_ |