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 | |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 43 | // Allows |this| to be passed as an argument in constructor initializer lists. |
| 44 | // This uses push/pop instead of the seemingly simpler suppress feature to avoid |
| 45 | // having the warning be disabled for more than just |code|. |
| 46 | // |
| 47 | // Example usage: |
| 48 | // Foo::Foo() : x(NULL), ALLOW_THIS_IN_INITIALIZER_LIST(y(this)), z(3) {} |
| 49 | // |
| 50 | // Compiler warning C4355: 'this': used in base member initializer list: |
| 51 | // http://msdn.microsoft.com/en-us/library/3c594ae3(VS.80).aspx |
| 52 | #define ALLOW_THIS_IN_INITIALIZER_LIST(code) MSVC_PUSH_DISABLE_WARNING(4355) \ |
| 53 | code \ |
| 54 | MSVC_POP_WARNING() |
| 55 | |
rvargas@google.com | 1ed7f9a | 2011-05-19 03:29:36 +0900 | [diff] [blame] | 56 | // Allows exporting a class that inherits from a non-exported base class. |
| 57 | // This uses suppress instead of push/pop because the delimiter after the |
| 58 | // declaration (either "," or "{") has to be placed before the pop macro. |
| 59 | // |
| 60 | // Example usage: |
| 61 | // class EXPORT_API Foo : NON_EXPORTED_BASE(public Bar) { |
| 62 | // |
| 63 | // MSVC Compiler warning C4275: |
| 64 | // 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] | 65 | // Note that this is intended to be used only when no access to the base class' |
| 66 | // static data is done through derived classes or inline methods. For more info, |
| 67 | // 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] | 68 | #define NON_EXPORTED_BASE(code) MSVC_SUPPRESS_WARNING(4275) \ |
| 69 | code |
| 70 | |
agl@chromium.org | 1958301 | 2008-11-22 08:58:09 +0900 | [diff] [blame] | 71 | #else // Not MSVC |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 72 | |
| 73 | #define MSVC_SUPPRESS_WARNING(n) |
| 74 | #define MSVC_PUSH_DISABLE_WARNING(n) |
evanm@google.com | 5756c4f | 2008-09-17 04:28:06 +0900 | [diff] [blame] | 75 | #define MSVC_PUSH_WARNING_LEVEL(n) |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 76 | #define MSVC_POP_WARNING() |
deanm@chromium.org | 520ada6 | 2008-11-22 03:48:52 +0900 | [diff] [blame] | 77 | #define MSVC_DISABLE_OPTIMIZE() |
| 78 | #define MSVC_ENABLE_OPTIMIZE() |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 79 | #define ALLOW_THIS_IN_INITIALIZER_LIST(code) code |
rvargas@google.com | 1ed7f9a | 2011-05-19 03:29:36 +0900 | [diff] [blame] | 80 | #define NON_EXPORTED_BASE(code) code |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 81 | |
| 82 | #endif // COMPILER_MSVC |
| 83 | |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 84 | |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 85 | // Annotate a variable indicating it's ok if the variable is not used. |
| 86 | // (Typically used to silence a compiler warning when the assignment |
| 87 | // is important for some other reason.) |
| 88 | // Use like: |
| 89 | // int x ALLOW_UNUSED = ...; |
agl@chromium.org | 1958301 | 2008-11-22 08:58:09 +0900 | [diff] [blame] | 90 | #if defined(COMPILER_GCC) |
phajdan.jr@chromium.org | 81427ab | 2008-12-18 03:30:28 +0900 | [diff] [blame] | 91 | #define ALLOW_UNUSED __attribute__((unused)) |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 92 | #else |
| 93 | #define ALLOW_UNUSED |
eroman@chromium.org | f1ab5fe | 2012-02-14 10:12:12 +0900 | [diff] [blame] | 94 | #endif |
| 95 | |
| 96 | // Annotate a function indicating it should not be inlined. |
| 97 | // Use like: |
| 98 | // NOINLINE void DoStuff() { ... } |
| 99 | #if defined(COMPILER_GCC) |
| 100 | #define NOINLINE __attribute__((noinline)) |
| 101 | #elif defined(COMPILER_MSVC) |
| 102 | #define NOINLINE __declspec(noinline) |
| 103 | #else |
davemoore@chromium.org | f895b37 | 2011-05-10 05:11:01 +0900 | [diff] [blame] | 104 | #define NOINLINE |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 105 | #endif |
| 106 | |
jbates@chromium.org | dde8e30 | 2012-02-24 02:52:20 +0900 | [diff] [blame] | 107 | // Specify memory alignment for structs, classes, etc. |
| 108 | // Use like: |
| 109 | // class ALIGNAS(16) MyClass { ... } |
| 110 | // ALIGNAS(16) int array[4]; |
| 111 | #if defined(COMPILER_MSVC) |
| 112 | #define ALIGNAS(byte_alignment) __declspec(align(byte_alignment)) |
| 113 | #elif defined(COMPILER_GCC) |
| 114 | #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment))) |
| 115 | #endif |
| 116 | |
dalecurtis@chromium.org | ee81643 | 2012-07-26 11:22:39 +0900 | [diff] [blame] | 117 | // Return the byte alignment of the given type (available at compile time). Use |
| 118 | // sizeof(type) prior to checking __alignof to workaround Visual C++ bug: |
| 119 | // http://goo.gl/isH0C |
jbates@chromium.org | dde8e30 | 2012-02-24 02:52:20 +0900 | [diff] [blame] | 120 | // Use like: |
| 121 | // ALIGNOF(int32) // this would be 4 |
| 122 | #if defined(COMPILER_MSVC) |
dalecurtis@chromium.org | ee81643 | 2012-07-26 11:22:39 +0900 | [diff] [blame] | 123 | #define ALIGNOF(type) (sizeof(type) - sizeof(type) + __alignof(type)) |
jbates@chromium.org | dde8e30 | 2012-02-24 02:52:20 +0900 | [diff] [blame] | 124 | #elif defined(COMPILER_GCC) |
| 125 | #define ALIGNOF(type) __alignof__(type) |
| 126 | #endif |
| 127 | |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 128 | // Annotate a virtual method indicating it must be overriding a virtual |
| 129 | // method in the parent class. |
| 130 | // Use like: |
| 131 | // virtual void foo() OVERRIDE; |
| 132 | #if defined(COMPILER_MSVC) |
| 133 | #define OVERRIDE override |
thakis@chromium.org | 54ad902 | 2011-02-08 02:09:03 +0900 | [diff] [blame] | 134 | #elif defined(__clang__) |
| 135 | #define OVERRIDE override |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 136 | #else |
| 137 | #define OVERRIDE |
| 138 | #endif |
| 139 | |
| 140 | // Annotate a function indicating the caller must examine the return value. |
| 141 | // Use like: |
| 142 | // int foo() WARN_UNUSED_RESULT; |
shess@chromium.org | 0da7dbd | 2011-05-20 09:48:35 +0900 | [diff] [blame] | 143 | // To explicitly ignore a result, see |ignore_result()| in <base/basictypes.h>. |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 144 | #if defined(COMPILER_GCC) |
agl@chromium.org | 1958301 | 2008-11-22 08:58:09 +0900 | [diff] [blame] | 145 | #define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 146 | #else |
| 147 | #define WARN_UNUSED_RESULT |
| 148 | #endif |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 149 | |
| 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.) |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 155 | #if defined(COMPILER_GCC) |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 156 | #define PRINTF_FORMAT(format_param, dots_param) \ |
| 157 | __attribute__((format(printf, format_param, dots_param))) |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 158 | #else |
| 159 | #define PRINTF_FORMAT(format_param, dots_param) |
| 160 | #endif |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 161 | |
| 162 | // WPRINTF_FORMAT is the same, but for wide format strings. |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 163 | // This doesn't appear to yet be implemented in any compiler. |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 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))) |
| 168 | |
agl@chromium.org | 1958301 | 2008-11-22 08:58:09 +0900 | [diff] [blame] | 169 | #endif // BASE_COMPILER_SPECIFIC_H_ |