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_
9 #include "build/build_config.h"
11 #if defined(COMPILER_MSVC)
13 // Macros for suppressing and disabling warnings on MSVC.
15 // Warning numbers are enumerated at:
16 // http://msdn.microsoft.com/en-us/library/8x5x43k7(VS.80).aspx
18 // The warning pragma:
19 // http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx
21 // Using __pragma instead of #pragma inside macros:
22 // http://msdn.microsoft.com/en-us/library/d9x1s805.aspx
24 // MSVC_SUPPRESS_WARNING disables warning |n| for the remainder of the line and
25 // for the next line of the source file.
26 #define MSVC_SUPPRESS_WARNING(n) __pragma(warning(suppress:n))
28 // MSVC_PUSH_DISABLE_WARNING pushes |n| onto a stack of warnings to be disabled.
29 // The warning remains disabled until popped by MSVC_POP_WARNING.
30 #define MSVC_PUSH_DISABLE_WARNING(n) __pragma(warning(push)) \
31 __pragma(warning(disable:n))
33 // MSVC_PUSH_WARNING_LEVEL pushes |n| as the global warning level. The level
34 // remains in effect until popped by MSVC_POP_WARNING(). Use 0 to disable all
36 #define MSVC_PUSH_WARNING_LEVEL(n) __pragma(warning(push, n))
38 // Pop effects of innermost MSVC_PUSH_* macro.
39 #define MSVC_POP_WARNING() __pragma(warning(pop))
41 #define MSVC_DISABLE_OPTIMIZE() __pragma(optimize("", off))
42 #define MSVC_ENABLE_OPTIMIZE() __pragma(optimize("", on))
44 // Allows |this| to be passed as an argument in constructor initializer lists.
45 // This uses push/pop instead of the seemingly simpler suppress feature to avoid
46 // having the warning be disabled for more than just |code|.
49 // Foo::Foo() : x(NULL), ALLOW_THIS_IN_INITIALIZER_LIST(y(this)), z(3) {}
51 // Compiler warning C4355: 'this': used in base member initializer list:
52 // http://msdn.microsoft.com/en-us/library/3c594ae3(VS.80).aspx
53 #define ALLOW_THIS_IN_INITIALIZER_LIST(code) MSVC_PUSH_DISABLE_WARNING(4355) \
57 // Allows exporting a class that inherits from a non-exported base class.
58 // This uses suppress instead of push/pop because the delimiter after the
59 // declaration (either "," or "{") has to be placed before the pop macro.
62 // class EXPORT_API Foo : NON_EXPORTED_BASE(public Bar) {
64 // MSVC Compiler warning C4275:
65 // non dll-interface class 'Bar' used as base for dll-interface class 'Foo'.
66 // Note that this is intended to be used only when no access to the base class'
67 // static data is done through derived classes or inline methods. For more info,
68 // see http://msdn.microsoft.com/en-us/library/3tdb471s(VS.80).aspx
69 #define NON_EXPORTED_BASE(code) MSVC_SUPPRESS_WARNING(4275) \
74 #define MSVC_SUPPRESS_WARNING(n)
75 #define MSVC_PUSH_DISABLE_WARNING(n)
76 #define MSVC_PUSH_WARNING_LEVEL(n)
77 #define MSVC_POP_WARNING()
78 #define MSVC_DISABLE_OPTIMIZE()
79 #define MSVC_ENABLE_OPTIMIZE()
80 #define ALLOW_THIS_IN_INITIALIZER_LIST(code) code
81 #define NON_EXPORTED_BASE(code) code
83 #endif // COMPILER_MSVC
86 // Annotate a variable indicating it's ok if the variable is not used.
87 // (Typically used to silence a compiler warning when the assignment
88 // is important for some other reason.)
90 // int x ALLOW_UNUSED = ...;
91 #if defined(COMPILER_GCC)
92 #define ALLOW_UNUSED __attribute__((unused))
97 // Annotate a function indicating it should not be inlined.
99 // NOINLINE void DoStuff() { ... }
100 #if defined(COMPILER_GCC)
101 #define NOINLINE __attribute__((noinline))
102 #elif defined(COMPILER_MSVC)
103 #define NOINLINE __declspec(noinline)
108 // Specify memory alignment for structs, classes, etc.
110 // class ALIGNAS(16) MyClass { ... }
111 // ALIGNAS(16) int array[4];
112 #if defined(COMPILER_MSVC)
113 #define ALIGNAS(byte_alignment) __declspec(align(byte_alignment))
114 #elif defined(COMPILER_GCC)
115 #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
118 // Return the byte alignment of the given type (available at compile time).
120 // ALIGNOF(int32) // this would be 4
121 #if defined(COMPILER_MSVC)
122 #define ALIGNOF(type) __alignof(type)
123 #elif defined(COMPILER_GCC)
124 #define ALIGNOF(type) __alignof__(type)
127 // Annotate a virtual method indicating it must be overriding a virtual
128 // method in the parent class.
130 // virtual void foo() OVERRIDE;
131 #if defined(COMPILER_MSVC)
132 #define OVERRIDE override
133 #elif defined(__clang__)
134 #define OVERRIDE override
139 // Annotate a function indicating the caller must examine the return value.
141 // int foo() WARN_UNUSED_RESULT;
142 // To explicitly ignore a result, see |ignore_result()| in <base/basictypes.h>.
143 #if defined(COMPILER_GCC)
144 #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
146 #define WARN_UNUSED_RESULT
149 // Tell the compiler a function is using a printf-style format string.
150 // |format_param| is the one-based index of the format string parameter;
151 // |dots_param| is the one-based index of the "..." parameter.
152 // For v*printf functions (which take a va_list), pass 0 for dots_param.
153 // (This is undocumented but matches what the system C headers do.)
154 #if defined(COMPILER_GCC)
155 #define PRINTF_FORMAT(format_param, dots_param) \
156 __attribute__((format(printf, format_param, dots_param)))
158 #define PRINTF_FORMAT(format_param, dots_param)
161 // WPRINTF_FORMAT is the same, but for wide format strings.
162 // This doesn't appear to yet be implemented in any compiler.
163 // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 .
164 #define WPRINTF_FORMAT(format_param, dots_param)
165 // If available, it would look like:
166 // __attribute__((format(wprintf, format_param, dots_param)))
168 #endif // BASE_COMPILER_SPECIFIC_H_