[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 | |
| 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)) |
[email protected] | b91902d | 2008-09-16 19:28:06 | [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. |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 38 | #define MSVC_POP_WARNING() __pragma(warning(pop)) |
| 39 | |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 40 | #define MSVC_DISABLE_OPTIMIZE() __pragma(optimize("", off)) |
| 41 | #define MSVC_ENABLE_OPTIMIZE() __pragma(optimize("", on)) |
| 42 | |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [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'. |
[email protected] | bfa08197 | 2012-02-09 06:48:09 | [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 |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame] | 55 | #define NON_EXPORTED_BASE(code) MSVC_SUPPRESS_WARNING(4275) \ |
| 56 | code |
| 57 | |
[email protected] | dd9afc0b | 2008-11-21 23:58:09 | [diff] [blame] | 58 | #else // Not MSVC |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 59 | |
| 60 | #define MSVC_SUPPRESS_WARNING(n) |
| 61 | #define MSVC_PUSH_DISABLE_WARNING(n) |
[email protected] | b91902d | 2008-09-16 19:28:06 | [diff] [blame] | 62 | #define MSVC_PUSH_WARNING_LEVEL(n) |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 63 | #define MSVC_POP_WARNING() |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 64 | #define MSVC_DISABLE_OPTIMIZE() |
| 65 | #define MSVC_ENABLE_OPTIMIZE() |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame] | 66 | #define NON_EXPORTED_BASE(code) code |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 67 | |
| 68 | #endif // COMPILER_MSVC |
| 69 | |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 70 | |
[email protected] | ccf8453d | 2013-11-07 17:49:50 | [diff] [blame] | 71 | // The C++ standard requires that static const members have an out-of-class |
| 72 | // definition (in a single compilation unit), but MSVC chokes on this (when |
| 73 | // language extensions, which are required, are enabled). (You're only likely to |
| 74 | // notice the need for a definition if you take the address of the member or, |
| 75 | // more commonly, pass it to a function that takes it as a reference argument -- |
| 76 | // probably an STL function.) This macro makes MSVC do the right thing. See |
| 77 | // http://msdn.microsoft.com/en-us/library/34h23df8(v=vs.100).aspx for more |
| 78 | // information. Use like: |
| 79 | // |
| 80 | // In .h file: |
| 81 | // struct Foo { |
| 82 | // static const int kBar = 5; |
| 83 | // }; |
| 84 | // |
| 85 | // In .cc file: |
| 86 | // STATIC_CONST_MEMBER_DEFINITION const int Foo::kBar; |
| 87 | #if defined(COMPILER_MSVC) |
| 88 | #define STATIC_CONST_MEMBER_DEFINITION __declspec(selectany) |
| 89 | #else |
| 90 | #define STATIC_CONST_MEMBER_DEFINITION |
| 91 | #endif |
| 92 | |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 93 | // Annotate a variable indicating it's ok if the variable is not used. |
| 94 | // (Typically used to silence a compiler warning when the assignment |
| 95 | // is important for some other reason.) |
| 96 | // Use like: |
pkasting | 99867ef | 2014-10-16 23:49:24 | [diff] [blame] | 97 | // int x = ...; |
| 98 | // ALLOW_UNUSED_LOCAL(x); |
| 99 | #define ALLOW_UNUSED_LOCAL(x) false ? (void)x : (void)0 |
| 100 | |
| 101 | // Annotate a typedef or function indicating it's ok if it's not used. |
| 102 | // Use like: |
| 103 | // typedef Foo Bar ALLOW_UNUSED_TYPE; |
[email protected] | dd9afc0b | 2008-11-21 23:58:09 | [diff] [blame] | 104 | #if defined(COMPILER_GCC) |
pkasting | 99867ef | 2014-10-16 23:49:24 | [diff] [blame] | 105 | #define ALLOW_UNUSED_TYPE __attribute__((unused)) |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 106 | #else |
pkasting | 99867ef | 2014-10-16 23:49:24 | [diff] [blame] | 107 | #define ALLOW_UNUSED_TYPE |
[email protected] | 2149cc62 | 2012-02-14 01:12:12 | [diff] [blame] | 108 | #endif |
| 109 | |
| 110 | // Annotate a function indicating it should not be inlined. |
| 111 | // Use like: |
| 112 | // NOINLINE void DoStuff() { ... } |
| 113 | #if defined(COMPILER_GCC) |
| 114 | #define NOINLINE __attribute__((noinline)) |
| 115 | #elif defined(COMPILER_MSVC) |
| 116 | #define NOINLINE __declspec(noinline) |
| 117 | #else |
[email protected] | 50795a0 | 2011-05-09 20:11:01 | [diff] [blame] | 118 | #define NOINLINE |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 119 | #endif |
| 120 | |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 121 | // Specify memory alignment for structs, classes, etc. |
| 122 | // Use like: |
| 123 | // class ALIGNAS(16) MyClass { ... } |
| 124 | // ALIGNAS(16) int array[4]; |
| 125 | #if defined(COMPILER_MSVC) |
| 126 | #define ALIGNAS(byte_alignment) __declspec(align(byte_alignment)) |
| 127 | #elif defined(COMPILER_GCC) |
| 128 | #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment))) |
| 129 | #endif |
| 130 | |
[email protected] | 9f01b02 | 2012-07-26 02:22:39 | [diff] [blame] | 131 | // Return the byte alignment of the given type (available at compile time). Use |
| 132 | // sizeof(type) prior to checking __alignof to workaround Visual C++ bug: |
| 133 | // http://goo.gl/isH0C |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 134 | // Use like: |
| 135 | // ALIGNOF(int32) // this would be 4 |
| 136 | #if defined(COMPILER_MSVC) |
[email protected] | 9f01b02 | 2012-07-26 02:22:39 | [diff] [blame] | 137 | #define ALIGNOF(type) (sizeof(type) - sizeof(type) + __alignof(type)) |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 138 | #elif defined(COMPILER_GCC) |
| 139 | #define ALIGNOF(type) __alignof__(type) |
| 140 | #endif |
| 141 | |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 142 | // Annotate a function indicating the caller must examine the return value. |
| 143 | // Use like: |
| 144 | // int foo() WARN_UNUSED_RESULT; |
[email protected] | 117bbe3 | 2011-05-20 00:48:35 | [diff] [blame] | 145 | // To explicitly ignore a result, see |ignore_result()| in <base/basictypes.h>. |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 146 | #if defined(COMPILER_GCC) |
[email protected] | dd9afc0b | 2008-11-21 23:58:09 | [diff] [blame] | 147 | #define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 148 | #else |
| 149 | #define WARN_UNUSED_RESULT |
| 150 | #endif |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 151 | |
| 152 | // Tell the compiler a function is using a printf-style format string. |
| 153 | // |format_param| is the one-based index of the format string parameter; |
| 154 | // |dots_param| is the one-based index of the "..." parameter. |
| 155 | // For v*printf functions (which take a va_list), pass 0 for dots_param. |
| 156 | // (This is undocumented but matches what the system C headers do.) |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 157 | #if defined(COMPILER_GCC) |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 158 | #define PRINTF_FORMAT(format_param, dots_param) \ |
| 159 | __attribute__((format(printf, format_param, dots_param))) |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 160 | #else |
| 161 | #define PRINTF_FORMAT(format_param, dots_param) |
| 162 | #endif |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 163 | |
| 164 | // WPRINTF_FORMAT is the same, but for wide format strings. |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 165 | // This doesn't appear to yet be implemented in any compiler. |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 166 | // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 . |
| 167 | #define WPRINTF_FORMAT(format_param, dots_param) |
| 168 | // If available, it would look like: |
| 169 | // __attribute__((format(wprintf, format_param, dots_param))) |
| 170 | |
[email protected] | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 171 | // MemorySanitizer annotations. |
[email protected] | 36c2b137 | 2014-02-07 18:54:44 | [diff] [blame] | 172 | #if defined(MEMORY_SANITIZER) && !defined(OS_NACL) |
[email protected] | eb82dfb | 2014-02-03 19:51:17 | [diff] [blame] | 173 | #include <sanitizer/msan_interface.h> |
[email protected] | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 174 | |
| 175 | // Mark a memory region fully initialized. |
| 176 | // Use this to annotate code that deliberately reads uninitialized data, for |
| 177 | // example a GC scavenging root set pointers from the stack. |
thestig | 1a42b407 | 2015-03-16 22:36:55 | [diff] [blame^] | 178 | #define MSAN_UNPOISON(p, size) __msan_unpoison(p, size) |
| 179 | |
| 180 | // Check a memory region for initializedness, as if it was being used here. |
| 181 | // If any bits are uninitialized, crash with an MSan report. |
| 182 | // Use this to sanitize data which MSan won't be able to track, e.g. before |
| 183 | // passing data to another process via shared memory. |
| 184 | #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) \ |
| 185 | __msan_check_mem_is_initialized(p, size) |
[email protected] | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 186 | #else // MEMORY_SANITIZER |
thestig | 1a42b407 | 2015-03-16 22:36:55 | [diff] [blame^] | 187 | #define MSAN_UNPOISON(p, size) |
| 188 | #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) |
[email protected] | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 189 | #endif // MEMORY_SANITIZER |
| 190 | |
[email protected] | 5a8d4ce | 2013-12-18 17:42:27 | [diff] [blame] | 191 | // Macro useful for writing cross-platform function pointers. |
| 192 | #if !defined(CDECL) |
| 193 | #if defined(OS_WIN) |
| 194 | #define CDECL __cdecl |
| 195 | #else // defined(OS_WIN) |
| 196 | #define CDECL |
| 197 | #endif // defined(OS_WIN) |
| 198 | #endif // !defined(CDECL) |
| 199 | |
[email protected] | 2bc0c699 | 2014-02-13 16:11:04 | [diff] [blame] | 200 | // Macro for hinting that an expression is likely to be false. |
| 201 | #if !defined(UNLIKELY) |
| 202 | #if defined(COMPILER_GCC) |
| 203 | #define UNLIKELY(x) __builtin_expect(!!(x), 0) |
| 204 | #else |
| 205 | #define UNLIKELY(x) (x) |
| 206 | #endif // defined(COMPILER_GCC) |
| 207 | #endif // !defined(UNLIKELY) |
| 208 | |
[email protected] | dd9afc0b | 2008-11-21 23:58:09 | [diff] [blame] | 209 | #endif // BASE_COMPILER_SPECIFIC_H_ |