davemoore@chromium.org | f895b37 | 2011-05-10 05:11:01 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 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_ |
thakis@chromium.org | 01d1452 | 2010-07-27 08:08:24 +0900 | [diff] [blame] | 7 | #pragma once |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 8 | |
| 9 | #include "build/build_config.h" |
| 10 | |
| 11 | #if defined(COMPILER_MSVC) |
| 12 | |
| 13 | // Macros for suppressing and disabling warnings on MSVC. |
| 14 | // |
| 15 | // Warning numbers are enumerated at: |
| 16 | // http://msdn.microsoft.com/en-us/library/8x5x43k7(VS.80).aspx |
| 17 | // |
| 18 | // The warning pragma: |
| 19 | // http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx |
| 20 | // |
| 21 | // Using __pragma instead of #pragma inside macros: |
| 22 | // http://msdn.microsoft.com/en-us/library/d9x1s805.aspx |
| 23 | |
| 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)) |
| 27 | |
| 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)) |
evanm@google.com | 5756c4f | 2008-09-17 04:28:06 +0900 | [diff] [blame] | 32 | |
| 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 |
| 35 | // warnings. |
| 36 | #define MSVC_PUSH_WARNING_LEVEL(n) __pragma(warning(push, n)) |
| 37 | |
| 38 | // Pop effects of innermost MSVC_PUSH_* macro. |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 39 | #define MSVC_POP_WARNING() __pragma(warning(pop)) |
| 40 | |
deanm@chromium.org | 520ada6 | 2008-11-22 03:48:52 +0900 | [diff] [blame] | 41 | #define MSVC_DISABLE_OPTIMIZE() __pragma(optimize("", off)) |
| 42 | #define MSVC_ENABLE_OPTIMIZE() __pragma(optimize("", on)) |
| 43 | |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 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|. |
| 47 | // |
| 48 | // Example usage: |
| 49 | // Foo::Foo() : x(NULL), ALLOW_THIS_IN_INITIALIZER_LIST(y(this)), z(3) {} |
| 50 | // |
| 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) \ |
| 54 | code \ |
| 55 | MSVC_POP_WARNING() |
| 56 | |
rvargas@google.com | 1ed7f9a | 2011-05-19 03:29:36 +0900 | [diff] [blame] | 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. |
| 60 | // |
| 61 | // Example usage: |
| 62 | // class EXPORT_API Foo : NON_EXPORTED_BASE(public Bar) { |
| 63 | // |
| 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 | // can be gained through the derived class. For more info, see |
| 68 | // http://msdn.microsoft.com/en-us/library/3tdb471s(VS.80).aspx |
| 69 | #define NON_EXPORTED_BASE(code) MSVC_SUPPRESS_WARNING(4275) \ |
| 70 | code |
| 71 | |
agl@chromium.org | 1958301 | 2008-11-22 08:58:09 +0900 | [diff] [blame] | 72 | #else // Not MSVC |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 73 | |
| 74 | #define MSVC_SUPPRESS_WARNING(n) |
| 75 | #define MSVC_PUSH_DISABLE_WARNING(n) |
evanm@google.com | 5756c4f | 2008-09-17 04:28:06 +0900 | [diff] [blame] | 76 | #define MSVC_PUSH_WARNING_LEVEL(n) |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 77 | #define MSVC_POP_WARNING() |
deanm@chromium.org | 520ada6 | 2008-11-22 03:48:52 +0900 | [diff] [blame] | 78 | #define MSVC_DISABLE_OPTIMIZE() |
| 79 | #define MSVC_ENABLE_OPTIMIZE() |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 80 | #define ALLOW_THIS_IN_INITIALIZER_LIST(code) code |
rvargas@google.com | 1ed7f9a | 2011-05-19 03:29:36 +0900 | [diff] [blame] | 81 | #define NON_EXPORTED_BASE(code) code |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 82 | |
| 83 | #endif // COMPILER_MSVC |
| 84 | |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 85 | |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 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.) |
| 89 | // Use like: |
| 90 | // int x ALLOW_UNUSED = ...; |
agl@chromium.org | 1958301 | 2008-11-22 08:58:09 +0900 | [diff] [blame] | 91 | #if defined(COMPILER_GCC) |
phajdan.jr@chromium.org | 81427ab | 2008-12-18 03:30:28 +0900 | [diff] [blame] | 92 | #define ALLOW_UNUSED __attribute__((unused)) |
davemoore@chromium.org | f895b37 | 2011-05-10 05:11:01 +0900 | [diff] [blame] | 93 | #define NOINLINE __attribute__((noinline)) |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 94 | #else |
| 95 | #define ALLOW_UNUSED |
davemoore@chromium.org | f895b37 | 2011-05-10 05:11:01 +0900 | [diff] [blame] | 96 | #define NOINLINE |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 97 | #endif |
| 98 | |
| 99 | // Annotate a virtual method indicating it must be overriding a virtual |
| 100 | // method in the parent class. |
| 101 | // Use like: |
| 102 | // virtual void foo() OVERRIDE; |
| 103 | #if defined(COMPILER_MSVC) |
| 104 | #define OVERRIDE override |
thakis@chromium.org | 54ad902 | 2011-02-08 02:09:03 +0900 | [diff] [blame] | 105 | #elif defined(__clang__) |
| 106 | #define OVERRIDE override |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 107 | #else |
| 108 | #define OVERRIDE |
| 109 | #endif |
| 110 | |
| 111 | // Annotate a function indicating the caller must examine the return value. |
| 112 | // Use like: |
| 113 | // int foo() WARN_UNUSED_RESULT; |
shess@chromium.org | 0da7dbd | 2011-05-20 09:48:35 +0900 | [diff] [blame] | 114 | // 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] | 115 | #if defined(COMPILER_GCC) |
agl@chromium.org | 1958301 | 2008-11-22 08:58:09 +0900 | [diff] [blame] | 116 | #define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 117 | #else |
| 118 | #define WARN_UNUSED_RESULT |
| 119 | #endif |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 120 | |
| 121 | // Tell the compiler a function is using a printf-style format string. |
| 122 | // |format_param| is the one-based index of the format string parameter; |
| 123 | // |dots_param| is the one-based index of the "..." parameter. |
| 124 | // For v*printf functions (which take a va_list), pass 0 for dots_param. |
| 125 | // (This is undocumented but matches what the system C headers do.) |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 126 | #if defined(COMPILER_GCC) |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 127 | #define PRINTF_FORMAT(format_param, dots_param) \ |
| 128 | __attribute__((format(printf, format_param, dots_param))) |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 129 | #else |
| 130 | #define PRINTF_FORMAT(format_param, dots_param) |
| 131 | #endif |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 132 | |
| 133 | // WPRINTF_FORMAT is the same, but for wide format strings. |
evan@chromium.org | 6b68db3 | 2010-10-09 01:20:32 +0900 | [diff] [blame] | 134 | // This doesn't appear to yet be implemented in any compiler. |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 135 | // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 . |
| 136 | #define WPRINTF_FORMAT(format_param, dots_param) |
| 137 | // If available, it would look like: |
| 138 | // __attribute__((format(wprintf, format_param, dots_param))) |
| 139 | |
agl@chromium.org | 1958301 | 2008-11-22 08:58:09 +0900 | [diff] [blame] | 140 | #endif // BASE_COMPILER_SPECIFIC_H_ |