blob: 23b9f1242364c9708c8cdadb58476a821f84ccd8 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
[email protected]f1ea2fa2008-08-21 22:26:064
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))
[email protected]b91902d2008-09-16 19:28:0631
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.
[email protected]f1ea2fa2008-08-21 22:26:0638#define MSVC_POP_WARNING() __pragma(warning(pop))
39
[email protected]0eb9f4342008-11-21 18:48:5240#define MSVC_DISABLE_OPTIMIZE() __pragma(optimize("", off))
41#define MSVC_ENABLE_OPTIMIZE() __pragma(optimize("", on))
42
[email protected]f1ea2fa2008-08-21 22:26:0643// 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
[email protected]dd9afc0b2008-11-21 23:58:0956#else // Not MSVC
[email protected]f1ea2fa2008-08-21 22:26:0657
58#define MSVC_SUPPRESS_WARNING(n)
59#define MSVC_PUSH_DISABLE_WARNING(n)
[email protected]b91902d2008-09-16 19:28:0660#define MSVC_PUSH_WARNING_LEVEL(n)
[email protected]f1ea2fa2008-08-21 22:26:0661#define MSVC_POP_WARNING()
[email protected]0eb9f4342008-11-21 18:48:5262#define MSVC_DISABLE_OPTIMIZE()
63#define MSVC_ENABLE_OPTIMIZE()
[email protected]f1ea2fa2008-08-21 22:26:0664#define ALLOW_THIS_IN_INITIALIZER_LIST(code) code
65
66#endif // COMPILER_MSVC
67
license.botbf09a502008-08-24 00:55:5568
[email protected]dd9afc0b2008-11-21 23:58:0969#if defined(COMPILER_GCC)
[email protected]34b2b002009-11-20 06:53:2870
[email protected]85e0f1f2008-12-17 18:30:2871#define ALLOW_UNUSED __attribute__((unused))
[email protected]dd9afc0b2008-11-21 23:58:0972#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
[email protected]34b2b002009-11-20 06:53:2873
74// Tell the compiler a function is using a printf-style format string.
75// |format_param| is the one-based index of the format string parameter;
76// |dots_param| is the one-based index of the "..." parameter.
77// For v*printf functions (which take a va_list), pass 0 for dots_param.
78// (This is undocumented but matches what the system C headers do.)
79#define PRINTF_FORMAT(format_param, dots_param) \
80 __attribute__((format(printf, format_param, dots_param)))
81
82// WPRINTF_FORMAT is the same, but for wide format strings.
83// This doesn't appear to yet be implemented.
84// See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 .
85#define WPRINTF_FORMAT(format_param, dots_param)
86// If available, it would look like:
87// __attribute__((format(wprintf, format_param, dots_param)))
88
[email protected]dd9afc0b2008-11-21 23:58:0989#else // Not GCC
[email protected]34b2b002009-11-20 06:53:2890
[email protected]85e0f1f2008-12-17 18:30:2891#define ALLOW_UNUSED
[email protected]dd9afc0b2008-11-21 23:58:0992#define WARN_UNUSED_RESULT
[email protected]34b2b002009-11-20 06:53:2893#define PRINTF_FORMAT(x, y)
94#define WPRINTF_FORMAT(x, y)
95
[email protected]dd9afc0b2008-11-21 23:58:0996#endif
97
98#endif // BASE_COMPILER_SPECIFIC_H_