[email protected] | 50795a0 | 2011-05-09 20:11:01 | [diff] [blame^] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 4 | |
| 5 | #ifndef BASE_COMPILER_SPECIFIC_H_ |
| 6 | #define BASE_COMPILER_SPECIFIC_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [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)) |
[email protected] | b91902d | 2008-09-16 19:28:06 | [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. |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 39 | #define MSVC_POP_WARNING() __pragma(warning(pop)) |
| 40 | |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 41 | #define MSVC_DISABLE_OPTIMIZE() __pragma(optimize("", off)) |
| 42 | #define MSVC_ENABLE_OPTIMIZE() __pragma(optimize("", on)) |
| 43 | |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [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 | |
[email protected] | dd9afc0b | 2008-11-21 23:58:09 | [diff] [blame] | 57 | #else // Not MSVC |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 58 | |
| 59 | #define MSVC_SUPPRESS_WARNING(n) |
| 60 | #define MSVC_PUSH_DISABLE_WARNING(n) |
[email protected] | b91902d | 2008-09-16 19:28:06 | [diff] [blame] | 61 | #define MSVC_PUSH_WARNING_LEVEL(n) |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 62 | #define MSVC_POP_WARNING() |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 63 | #define MSVC_DISABLE_OPTIMIZE() |
| 64 | #define MSVC_ENABLE_OPTIMIZE() |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 65 | #define ALLOW_THIS_IN_INITIALIZER_LIST(code) code |
| 66 | |
| 67 | #endif // COMPILER_MSVC |
| 68 | |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 69 | |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 70 | // Annotate a variable indicating it's ok if the variable is not used. |
| 71 | // (Typically used to silence a compiler warning when the assignment |
| 72 | // is important for some other reason.) |
| 73 | // Use like: |
| 74 | // int x ALLOW_UNUSED = ...; |
[email protected] | dd9afc0b | 2008-11-21 23:58:09 | [diff] [blame] | 75 | #if defined(COMPILER_GCC) |
[email protected] | 85e0f1f | 2008-12-17 18:30:28 | [diff] [blame] | 76 | #define ALLOW_UNUSED __attribute__((unused)) |
[email protected] | 50795a0 | 2011-05-09 20:11:01 | [diff] [blame^] | 77 | #define NOINLINE __attribute__((noinline)) |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 78 | #else |
| 79 | #define ALLOW_UNUSED |
[email protected] | 50795a0 | 2011-05-09 20:11:01 | [diff] [blame^] | 80 | #define NOINLINE |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 81 | #endif |
| 82 | |
| 83 | // Annotate a virtual method indicating it must be overriding a virtual |
| 84 | // method in the parent class. |
| 85 | // Use like: |
| 86 | // virtual void foo() OVERRIDE; |
| 87 | #if defined(COMPILER_MSVC) |
| 88 | #define OVERRIDE override |
[email protected] | ad67f97 | 2011-02-07 17:09:03 | [diff] [blame] | 89 | #elif defined(__clang__) |
| 90 | #define OVERRIDE override |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 91 | #else |
| 92 | #define OVERRIDE |
| 93 | #endif |
| 94 | |
| 95 | // Annotate a function indicating the caller must examine the return value. |
| 96 | // Use like: |
| 97 | // int foo() WARN_UNUSED_RESULT; |
| 98 | #if defined(COMPILER_GCC) |
[email protected] | dd9afc0b | 2008-11-21 23:58:09 | [diff] [blame] | 99 | #define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 100 | #else |
| 101 | #define WARN_UNUSED_RESULT |
| 102 | #endif |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 103 | |
| 104 | // Tell the compiler a function is using a printf-style format string. |
| 105 | // |format_param| is the one-based index of the format string parameter; |
| 106 | // |dots_param| is the one-based index of the "..." parameter. |
| 107 | // For v*printf functions (which take a va_list), pass 0 for dots_param. |
| 108 | // (This is undocumented but matches what the system C headers do.) |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 109 | #if defined(COMPILER_GCC) |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 110 | #define PRINTF_FORMAT(format_param, dots_param) \ |
| 111 | __attribute__((format(printf, format_param, dots_param))) |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 112 | #else |
| 113 | #define PRINTF_FORMAT(format_param, dots_param) |
| 114 | #endif |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 115 | |
| 116 | // WPRINTF_FORMAT is the same, but for wide format strings. |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 117 | // This doesn't appear to yet be implemented in any compiler. |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 118 | // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 . |
| 119 | #define WPRINTF_FORMAT(format_param, dots_param) |
| 120 | // If available, it would look like: |
| 121 | // __attribute__((format(wprintf, format_param, dots_param))) |
| 122 | |
[email protected] | dd9afc0b | 2008-11-21 23:58:09 | [diff] [blame] | 123 | #endif // BASE_COMPILER_SPECIFIC_H_ |