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