1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 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 // For _Printf_format_string_. 13 #include <sal.h> 14 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) \ 33 __pragma(warning(push)) __pragma(warning(disable : n)) 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. 41 #define MSVC_POP_WARNING() __pragma(warning(pop)) 42 43 #define MSVC_DISABLE_OPTIMIZE() __pragma(optimize("", off)) 44 #define MSVC_ENABLE_OPTIMIZE() __pragma(optimize("", on)) 45 46 // Allows exporting a class that inherits from a non-exported base class. 47 // This uses suppress instead of push/pop because the delimiter after the 48 // declaration (either "," or "{") has to be placed before the pop macro. 49 // 50 // Example usage: 51 // class EXPORT_API Foo : NON_EXPORTED_BASE(public Bar) { 52 // 53 // MSVC Compiler warning C4275: 54 // non dll-interface class 'Bar' used as base for dll-interface class 'Foo'. 55 // Note that this is intended to be used only when no access to the base class' 56 // static data is done through derived classes or inline methods. For more info, 57 // see http://msdn.microsoft.com/en-us/library/3tdb471s(VS.80).aspx 58 #define NON_EXPORTED_BASE(code) \ 59 MSVC_SUPPRESS_WARNING(4275) \ 60 code 61 62 #else // Not MSVC 63 64 #define _Printf_format_string_ 65 #define MSVC_SUPPRESS_WARNING(n) 66 #define MSVC_PUSH_DISABLE_WARNING(n) 67 #define MSVC_PUSH_WARNING_LEVEL(n) 68 #define MSVC_POP_WARNING() 69 #define MSVC_DISABLE_OPTIMIZE() 70 #define MSVC_ENABLE_OPTIMIZE() 71 #define NON_EXPORTED_BASE(code) code 72 73 #endif // COMPILER_MSVC 74 75 // Annotate a variable indicating it's ok if the variable is not used. 76 // (Typically used to silence a compiler warning when the assignment 77 // is important for some other reason.) 78 // Use like: 79 // int x = ...; 80 // ALLOW_UNUSED_LOCAL(x); 81 #define ALLOW_UNUSED_LOCAL(x) false ? (void)x : (void)0 82 83 // Annotate a typedef or function indicating it's ok if it's not used. 84 // Use like: 85 // typedef Foo Bar ALLOW_UNUSED_TYPE; 86 #if defined(COMPILER_GCC) || defined(__clang__) 87 #define ALLOW_UNUSED_TYPE __attribute__((unused)) 88 #else 89 #define ALLOW_UNUSED_TYPE 90 #endif 91 92 // Annotate a function indicating it should not be inlined. 93 // Use like: 94 // NOINLINE void DoStuff() { ... } 95 #if defined(COMPILER_GCC) 96 #define NOINLINE __attribute__((noinline)) 97 #elif defined(COMPILER_MSVC) 98 #define NOINLINE __declspec(noinline) 99 #else 100 #define NOINLINE 101 #endif 102 103 #if COMPILER_GCC && defined(NDEBUG) 104 #define ALWAYS_INLINE inline __attribute__((__always_inline__)) 105 #elif COMPILER_MSVC && defined(NDEBUG) 106 #define ALWAYS_INLINE __forceinline 107 #else 108 #define ALWAYS_INLINE inline 109 #endif 110 111 // Specify memory alignment for structs, classes, etc. 112 // Use like: 113 // class ALIGNAS(16) MyClass { ... } 114 // ALIGNAS(16) int array[4]; 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 121 // Return the byte alignment of the given type (available at compile time). 122 // Use like: 123 // ALIGNOF(int32_t) // this would be 4 124 #if defined(COMPILER_MSVC) 125 #define ALIGNOF(type) __alignof(type) 126 #elif defined(COMPILER_GCC) 127 #define ALIGNOF(type) __alignof__(type) 128 #endif 129 130 // Annotate a function indicating the caller must examine the return value. 131 // Use like: 132 // int foo() WARN_UNUSED_RESULT; 133 // To explicitly ignore a result, see |ignore_result()| in base/macros.h. 134 #undef WARN_UNUSED_RESULT 135 #if defined(COMPILER_GCC) || defined(__clang__) 136 #define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) 137 #else 138 #define WARN_UNUSED_RESULT 139 #endif 140 141 // Tell the compiler a function is using a printf-style format string. 142 // |format_param| is the one-based index of the format string parameter; 143 // |dots_param| is the one-based index of the "..." parameter. 144 // For v*printf functions (which take a va_list), pass 0 for dots_param. 145 // (This is undocumented but matches what the system C headers do.) 146 #if defined(COMPILER_GCC) || defined(__clang__) 147 #define PRINTF_FORMAT(format_param, dots_param) \ 148 __attribute__((format(printf, format_param, dots_param))) 149 #else 150 #define PRINTF_FORMAT(format_param, dots_param) 151 #endif 152 153 // WPRINTF_FORMAT is the same, but for wide format strings. 154 // This doesn't appear to yet be implemented in any compiler. 155 // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 . 156 #define WPRINTF_FORMAT(format_param, dots_param) 157 // If available, it would look like: 158 // __attribute__((format(wprintf, format_param, dots_param))) 159 160 // Sanitizers annotations. 161 #if defined(__has_attribute) 162 #if __has_attribute(no_sanitize) 163 #define NO_SANITIZE(what) __attribute__((no_sanitize(what))) 164 #endif 165 #endif 166 #if !defined(NO_SANITIZE) 167 #define NO_SANITIZE(what) 168 #endif 169 170 // MemorySanitizer annotations. 171 #if defined(MEMORY_SANITIZER) && !defined(OS_NACL) 172 #include <sanitizer/msan_interface.h> 173 174 // Mark a memory region fully initialized. 175 // Use this to annotate code that deliberately reads uninitialized data, for 176 // example a GC scavenging root set pointers from the stack. 177 #define MSAN_UNPOISON(p, size) __msan_unpoison(p, size) 178 179 // Check a memory region for initializedness, as if it was being used here. 180 // If any bits are uninitialized, crash with an MSan report. 181 // Use this to sanitize data which MSan won't be able to track, e.g. before 182 // passing data to another process via shared memory. 183 #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) \ 184 __msan_check_mem_is_initialized(p, size) 185 #else // MEMORY_SANITIZER 186 #define MSAN_UNPOISON(p, size) 187 #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) 188 #endif // MEMORY_SANITIZER 189 190 // DISABLE_CFI_PERF -- Disable Control Flow Integrity for perf reasons. 191 #if !defined(DISABLE_CFI_PERF) 192 #if defined(__clang__) && defined(OFFICIAL_BUILD) 193 #define DISABLE_CFI_PERF __attribute__((no_sanitize("cfi"))) 194 #else 195 #define DISABLE_CFI_PERF 196 #endif 197 #endif 198 199 // Macro useful for writing cross-platform function pointers. 200 #if !defined(CDECL) 201 #if defined(OS_WIN) 202 #define CDECL __cdecl 203 #else // defined(OS_WIN) 204 #define CDECL 205 #endif // defined(OS_WIN) 206 #endif // !defined(CDECL) 207 208 // Macro for hinting that an expression is likely to be false. 209 #if !defined(UNLIKELY) 210 #if defined(COMPILER_GCC) 211 #define UNLIKELY(x) __builtin_expect(!!(x), 0) 212 #else 213 #define UNLIKELY(x) (x) 214 #endif // defined(COMPILER_GCC) 215 #endif // !defined(UNLIKELY) 216 217 #if !defined(LIKELY) 218 #if defined(COMPILER_GCC) 219 #define LIKELY(x) __builtin_expect(!!(x), 1) 220 #else 221 #define LIKELY(x) (x) 222 #endif // defined(COMPILER_GCC) 223 #endif // !defined(LIKELY) 224 225 // Compiler feature-detection. 226 // clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension 227 #if defined(__has_feature) 228 #define HAS_FEATURE(FEATURE) __has_feature(FEATURE) 229 #else 230 #define HAS_FEATURE(FEATURE) 0 231 #endif 232 233 #endif // BASE_COMPILER_SPECIFIC_H_ 234