eroman@chromium.org | f1ab5fe | 2012-02-14 10:12:12 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [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 | |
| 12 | // Macros for suppressing and disabling warnings on MSVC. |
| 13 | // |
| 14 | // Warning numbers are enumerated at: |
| 15 | // http://msdn.microsoft.com/en-us/library/8x5x43k7(VS.80).aspx |
| 16 | // |
| 17 | // The warning pragma: |
| 18 | // http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx |
| 19 | // |
| 20 | // Using __pragma instead of #pragma inside macros: |
| 21 | // http://msdn.microsoft.com/en-us/library/d9x1s805.aspx |
| 22 | |
| 23 | // MSVC_SUPPRESS_WARNING disables warning |n| for the remainder of the line and |
| 24 | // for the next line of the source file. |
| 25 | #define MSVC_SUPPRESS_WARNING(n) __pragma(warning(suppress:n)) |
| 26 | |
| 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)) |
evanm@google.com | 5756c4f | 2008-09-17 04:28:06 +0900 | [diff] [blame] | 31 | |
| 32 | // MSVC_PUSH_WARNING_LEVEL pushes |n| as the global warning level. The level |
| 33 | // remains in effect until popped by MSVC_POP_WARNING(). Use 0 to disable all |
| 34 | // warnings. |
| 35 | #define MSVC_PUSH_WARNING_LEVEL(n) __pragma(warning(push, n)) |
| 36 | |
| 37 | // Pop effects of innermost MSVC_PUSH_* macro. |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 38 | #define MSVC_POP_WARNING() __pragma(warning(pop)) |
| 39 | |
deanm@chromium.org | 520ada6 | 2008-11-22 03:48:52 +0900 | [diff] [blame] | 40 | #define MSVC_DISABLE_OPTIMIZE() __pragma(optimize("", off)) |
| 41 | #define MSVC_ENABLE_OPTIMIZE() __pragma(optimize("", on)) |
| 42 | |
rvargas@google.com | 1ed7f9a | 2011-05-19 03:29:36 +0900 | [diff] [blame] | 43 | // Allows exporting a class that inherits from a non-exported base class. |
| 44 | // This uses suppress instead of push/pop because the delimiter after the |
| 45 | // declaration (either "," or "{") has to be placed before the pop macro. |
| 46 | // |
| 47 | // Example usage: |
| 48 | // class EXPORT_API Foo : NON_EXPORTED_BASE(public Bar) { |
| 49 | // |
| 50 | // MSVC Compiler warning C4275: |
| 51 | // non dll-interface class 'Bar' used as base for dll-interface class 'Foo'. |
jam@chromium.org | 743e2e3 | 2012-02-09 15:48:09 +0900 | [diff] [blame] | 52 | // Note that this is intended to be used only when no access to the base class' |
| 53 | // static data is done through derived classes or inline methods. For more info, |
| 54 | // see http://msdn.microsoft.com/en-us/library/3tdb471s(VS.80).aspx |
rvargas@google.com | 1ed7f9a | 2011-05-19 03:29:36 +0900 | [diff] [blame] | 55 | #define NON_EXPORTED_BASE(code) MSVC_SUPPRESS_WARNING(4275) \ |
| 56 | code |
| 57 | |
agl@chromium.org | 1958301 | 2008-11-22 08:58:09 +0900 | [diff] [blame] | 58 | #else // Not MSVC |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 59 | |
| 60 | #define MSVC_SUPPRESS_WARNING(n) |
| 61 | #define MSVC_PUSH_DISABLE_WARNING(n) |
evanm@google.com | 5756c4f | 2008-09-17 04:28:06 +0900 | [diff] [blame] | 62 | #define MSVC_PUSH_WARNING_LEVEL(n) |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 63 | #define MSVC_POP_WARNING() |
deanm@chromium.org | 520ada6 | 2008-11-22 03:48:52 +0900 | [diff] [blame] | 64 | #define MSVC_DISABLE_OPTIMIZE() |
| 65 | #define MSVC_ENABLE_OPTIMIZE() |
rvargas@google.com | 1ed7f9a | 2011-05-19 03:29:36 +0900 | [diff] [blame] | 66 | #define NON_EXPORTED_BASE(code) code |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 67 | |
| 68 | #endif // COMPILER_MSVC |
| 69 | |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 70 | |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 71 | // Annotate a variable indicating it's ok if the variable is not used. |
| 72 | // (Typically used to silence a compiler warning when the assignment |
| 73 | // is important for some other reason.) |
| 74 | // Use like: |
pkasting | d36037a | 2014-10-17 08:49:24 +0900 | [diff] [blame] | 75 | // int x = ...; |
| 76 | // ALLOW_UNUSED_LOCAL(x); |
| 77 | #define ALLOW_UNUSED_LOCAL(x) false ? (void)x : (void)0 |
| 78 | |
| 79 | // Annotate a typedef or function indicating it's ok if it's not used. |
| 80 | // Use like: |
| 81 | // typedef Foo Bar ALLOW_UNUSED_TYPE; |
thakis | ee90472 | 2015-07-21 03:10:55 +0900 | [diff] [blame] | 82 | #if defined(COMPILER_GCC) || defined(__clang__) |
pkasting | d36037a | 2014-10-17 08:49:24 +0900 | [diff] [blame] | 83 | #define ALLOW_UNUSED_TYPE __attribute__((unused)) |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 84 | #else |
pkasting | d36037a | 2014-10-17 08:49:24 +0900 | [diff] [blame] | 85 | #define ALLOW_UNUSED_TYPE |
eroman@chromium.org | f1ab5fe | 2012-02-14 10:12:12 +0900 | [diff] [blame] | 86 | #endif |
| 87 | |
| 88 | // Annotate a function indicating it should not be inlined. |
| 89 | // Use like: |
| 90 | // NOINLINE void DoStuff() { ... } |
| 91 | #if defined(COMPILER_GCC) |
| 92 | #define NOINLINE __attribute__((noinline)) |
| 93 | #elif defined(COMPILER_MSVC) |
| 94 | #define NOINLINE __declspec(noinline) |
| 95 | #else |
davemoore@chromium.org | f895b37 | 2011-05-10 05:11:01 +0900 | [diff] [blame] | 96 | #define NOINLINE |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 97 | #endif |
| 98 | |
jbates@chromium.org | dde8e30 | 2012-02-24 02:52:20 +0900 | [diff] [blame] | 99 | // Specify memory alignment for structs, classes, etc. |
| 100 | // Use like: |
| 101 | // class ALIGNAS(16) MyClass { ... } |
| 102 | // ALIGNAS(16) int array[4]; |
| 103 | #if defined(COMPILER_MSVC) |
| 104 | #define ALIGNAS(byte_alignment) __declspec(align(byte_alignment)) |
| 105 | #elif defined(COMPILER_GCC) |
| 106 | #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment))) |
| 107 | #endif |
| 108 | |
thakis | e0742f5 | 2015-04-09 04:34:29 +0900 | [diff] [blame] | 109 | // Return the byte alignment of the given type (available at compile time). |
jbates@chromium.org | dde8e30 | 2012-02-24 02:52:20 +0900 | [diff] [blame] | 110 | // Use like: |
| 111 | // ALIGNOF(int32) // this would be 4 |
| 112 | #if defined(COMPILER_MSVC) |
thakis | e0742f5 | 2015-04-09 04:34:29 +0900 | [diff] [blame] | 113 | #define ALIGNOF(type) __alignof(type) |
jbates@chromium.org | dde8e30 | 2012-02-24 02:52:20 +0900 | [diff] [blame] | 114 | #elif defined(COMPILER_GCC) |
| 115 | #define ALIGNOF(type) __alignof__(type) |
| 116 | #endif |
| 117 | |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 118 | // Annotate a function indicating the caller must examine the return value. |
| 119 | // Use like: |
| 120 | // int foo() WARN_UNUSED_RESULT; |
toyoshim | e187934 | 2015-07-03 17:38:07 +0900 | [diff] [blame] | 121 | // To explicitly ignore a result, see |ignore_result()| in base/macros.h. |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 122 | #if defined(COMPILER_GCC) |
agl@chromium.org | 1958301 | 2008-11-22 08:58:09 +0900 | [diff] [blame] | 123 | #define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 124 | #else |
| 125 | #define WARN_UNUSED_RESULT |
| 126 | #endif |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 127 | |
| 128 | // Tell the compiler a function is using a printf-style format string. |
| 129 | // |format_param| is the one-based index of the format string parameter; |
| 130 | // |dots_param| is the one-based index of the "..." parameter. |
| 131 | // For v*printf functions (which take a va_list), pass 0 for dots_param. |
| 132 | // (This is undocumented but matches what the system C headers do.) |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 133 | #if defined(COMPILER_GCC) |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 134 | #define PRINTF_FORMAT(format_param, dots_param) \ |
| 135 | __attribute__((format(printf, format_param, dots_param))) |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 136 | #else |
| 137 | #define PRINTF_FORMAT(format_param, dots_param) |
| 138 | #endif |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 139 | |
| 140 | // WPRINTF_FORMAT is the same, but for wide format strings. |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 141 | // This doesn't appear to yet be implemented in any compiler. |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 142 | // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 . |
| 143 | #define WPRINTF_FORMAT(format_param, dots_param) |
| 144 | // If available, it would look like: |
| 145 | // __attribute__((format(wprintf, format_param, dots_param))) |
| 146 | |
eugenis@chromium.org | 5eff831 | 2013-03-21 06:18:22 +0900 | [diff] [blame] | 147 | // MemorySanitizer annotations. |
earthdok@chromium.org | 815eb60 | 2014-02-08 03:54:44 +0900 | [diff] [blame] | 148 | #if defined(MEMORY_SANITIZER) && !defined(OS_NACL) |
earthdok@chromium.org | 0446a67 | 2014-02-04 04:51:17 +0900 | [diff] [blame] | 149 | #include <sanitizer/msan_interface.h> |
eugenis@chromium.org | 5eff831 | 2013-03-21 06:18:22 +0900 | [diff] [blame] | 150 | |
| 151 | // Mark a memory region fully initialized. |
| 152 | // Use this to annotate code that deliberately reads uninitialized data, for |
| 153 | // example a GC scavenging root set pointers from the stack. |
thestig | 2913d85 | 2015-03-17 07:36:55 +0900 | [diff] [blame] | 154 | #define MSAN_UNPOISON(p, size) __msan_unpoison(p, size) |
| 155 | |
| 156 | // Check a memory region for initializedness, as if it was being used here. |
| 157 | // If any bits are uninitialized, crash with an MSan report. |
| 158 | // Use this to sanitize data which MSan won't be able to track, e.g. before |
| 159 | // passing data to another process via shared memory. |
| 160 | #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) \ |
| 161 | __msan_check_mem_is_initialized(p, size) |
eugenis@chromium.org | 5eff831 | 2013-03-21 06:18:22 +0900 | [diff] [blame] | 162 | #else // MEMORY_SANITIZER |
thestig | 2913d85 | 2015-03-17 07:36:55 +0900 | [diff] [blame] | 163 | #define MSAN_UNPOISON(p, size) |
| 164 | #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) |
eugenis@chromium.org | 5eff831 | 2013-03-21 06:18:22 +0900 | [diff] [blame] | 165 | #endif // MEMORY_SANITIZER |
| 166 | |
jochen@chromium.org | 74e19e2 | 2013-12-19 02:42:27 +0900 | [diff] [blame] | 167 | // Macro useful for writing cross-platform function pointers. |
| 168 | #if !defined(CDECL) |
| 169 | #if defined(OS_WIN) |
| 170 | #define CDECL __cdecl |
| 171 | #else // defined(OS_WIN) |
| 172 | #define CDECL |
| 173 | #endif // defined(OS_WIN) |
| 174 | #endif // !defined(CDECL) |
| 175 | |
skyostil@chromium.org | 98e93c0 | 2014-02-14 01:11:04 +0900 | [diff] [blame] | 176 | // Macro for hinting that an expression is likely to be false. |
| 177 | #if !defined(UNLIKELY) |
| 178 | #if defined(COMPILER_GCC) |
| 179 | #define UNLIKELY(x) __builtin_expect(!!(x), 0) |
| 180 | #else |
| 181 | #define UNLIKELY(x) (x) |
| 182 | #endif // defined(COMPILER_GCC) |
| 183 | #endif // !defined(UNLIKELY) |
| 184 | |
agl@chromium.org | 1958301 | 2008-11-22 08:58:09 +0900 | [diff] [blame] | 185 | #endif // BASE_COMPILER_SPECIFIC_H_ |