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.
5 #ifndef BASE_COMPILER_SPECIFIC_H_
6 #define BASE_COMPILER_SPECIFIC_H_
8 #include "build/build_config.h"
10 #if defined(COMPILER_MSVC)
12 // Macros for suppressing and disabling warnings on MSVC.
14 // Warning numbers are enumerated at:
15 // http://msdn.microsoft.com/en-us/library/8x5x43k7(VS.80).aspx
17 // The warning pragma:
18 // http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx
20 // Using __pragma instead of #pragma inside macros:
21 // http://msdn.microsoft.com/en-us/library/d9x1s805.aspx
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))
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))
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
35 #define MSVC_PUSH_WARNING_LEVEL(n) __pragma(warning(push, n))
37 // Pop effects of innermost MSVC_PUSH_* macro.
38 #define MSVC_POP_WARNING() __pragma(warning(pop))
40 #define MSVC_DISABLE_OPTIMIZE() __pragma(optimize("", off))
41 #define MSVC_ENABLE_OPTIMIZE() __pragma(optimize("", on))
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.
48 // class EXPORT_API Foo : NON_EXPORTED_BASE(public Bar) {
50 // MSVC Compiler warning C4275:
51 // non dll-interface class 'Bar' used as base for dll-interface class 'Foo'.
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
55 #define NON_EXPORTED_BASE(code) MSVC_SUPPRESS_WARNING(4275) \
60 #define MSVC_SUPPRESS_WARNING(n)
61 #define MSVC_PUSH_DISABLE_WARNING(n)
62 #define MSVC_PUSH_WARNING_LEVEL(n)
63 #define MSVC_POP_WARNING()
64 #define MSVC_DISABLE_OPTIMIZE()
65 #define MSVC_ENABLE_OPTIMIZE()
66 #define NON_EXPORTED_BASE(code) code
68 #endif // COMPILER_MSVC
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:
82 // static const int kBar = 5;
86 // STATIC_CONST_MEMBER_DEFINITION const int Foo::kBar;
87 #if defined(COMPILER_MSVC)
88 #define STATIC_CONST_MEMBER_DEFINITION __declspec(selectany)
90 #define STATIC_CONST_MEMBER_DEFINITION
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.)
98 // ALLOW_UNUSED_LOCAL(x);
99 #define ALLOW_UNUSED_LOCAL(x) false ? (void)x : (void)0
101 // Annotate a typedef or function indicating it's ok if it's not used.
103 // typedef Foo Bar ALLOW_UNUSED_TYPE;
104 #if defined(COMPILER_GCC)
105 #define ALLOW_UNUSED_TYPE __attribute__((unused))
107 #define ALLOW_UNUSED_TYPE
110 // Annotate a function indicating it should not be inlined.
112 // NOINLINE void DoStuff() { ... }
113 #if defined(COMPILER_GCC)
114 #define NOINLINE __attribute__((noinline))
115 #elif defined(COMPILER_MSVC)
116 #define NOINLINE __declspec(noinline)
121 // Specify memory alignment for structs, classes, etc.
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)))
131 // Return the byte alignment of the given type (available at compile time).
133 // ALIGNOF(int32) // this would be 4
134 #if defined(COMPILER_MSVC)
135 #define ALIGNOF(type) __alignof(type)
136 #elif defined(COMPILER_GCC)
137 #define ALIGNOF(type) __alignof__(type)
140 // Annotate a function indicating the caller must examine the return value.
142 // int foo() WARN_UNUSED_RESULT;
143 // To explicitly ignore a result, see |ignore_result()| in <base/basictypes.h>.
144 #if defined(COMPILER_GCC)
145 #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
147 #define WARN_UNUSED_RESULT
150 // Tell the compiler a function is using a printf-style format string.
151 // |format_param| is the one-based index of the format string parameter;
152 // |dots_param| is the one-based index of the "..." parameter.
153 // For v*printf functions (which take a va_list), pass 0 for dots_param.
154 // (This is undocumented but matches what the system C headers do.)
155 #if defined(COMPILER_GCC)
156 #define PRINTF_FORMAT(format_param, dots_param) \
157 __attribute__((format(printf, format_param, dots_param)))
159 #define PRINTF_FORMAT(format_param, dots_param)
162 // WPRINTF_FORMAT is the same, but for wide format strings.
163 // This doesn't appear to yet be implemented in any compiler.
164 // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 .
165 #define WPRINTF_FORMAT(format_param, dots_param)
166 // If available, it would look like:
167 // __attribute__((format(wprintf, format_param, dots_param)))
169 // MemorySanitizer annotations.
170 #if defined(MEMORY_SANITIZER) && !defined(OS_NACL)
171 #include <sanitizer/msan_interface.h>
173 // Mark a memory region fully initialized.
174 // Use this to annotate code that deliberately reads uninitialized data, for
175 // example a GC scavenging root set pointers from the stack.
176 #define MSAN_UNPOISON(p, size) __msan_unpoison(p, size)
178 // Check a memory region for initializedness, as if it was being used here.
179 // If any bits are uninitialized, crash with an MSan report.
180 // Use this to sanitize data which MSan won't be able to track, e.g. before
181 // passing data to another process via shared memory.
182 #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) \
183 __msan_check_mem_is_initialized(p, size)
184 #else // MEMORY_SANITIZER
185 #define MSAN_UNPOISON(p, size)
186 #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size)
187 #endif // MEMORY_SANITIZER
189 // Macro useful for writing cross-platform function pointers.
192 #define CDECL __cdecl
193 #else // defined(OS_WIN)
195 #endif // defined(OS_WIN)
196 #endif // !defined(CDECL)
198 // Macro for hinting that an expression is likely to be false.
199 #if !defined(UNLIKELY)
200 #if defined(COMPILER_GCC)
201 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
203 #define UNLIKELY(x) (x)
204 #endif // defined(COMPILER_GCC)
205 #endif // !defined(UNLIKELY)
207 #endif // BASE_COMPILER_SPECIFIC_H_