blob: 0a6e05a6c67988b64b416b612d47ec41c457a78f [file] [log] [blame]
davemoore@chromium.orgf895b372011-05-10 05:11:01 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
mmentovai@google.comfa5f9932008-08-22 07:26:06 +09004
5#ifndef BASE_COMPILER_SPECIFIC_H_
6#define BASE_COMPILER_SPECIFIC_H_
thakis@chromium.org01d14522010-07-27 08:08:24 +09007#pragma once
mmentovai@google.comfa5f9932008-08-22 07:26:06 +09008
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.com5756c4f2008-09-17 04:28:06 +090032
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.comfa5f9932008-08-22 07:26:06 +090039#define MSVC_POP_WARNING() __pragma(warning(pop))
40
deanm@chromium.org520ada62008-11-22 03:48:52 +090041#define MSVC_DISABLE_OPTIMIZE() __pragma(optimize("", off))
42#define MSVC_ENABLE_OPTIMIZE() __pragma(optimize("", on))
43
mmentovai@google.comfa5f9932008-08-22 07:26:06 +090044// 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.com1ed7f9a2011-05-19 03:29:36 +090057// 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.org19583012008-11-22 08:58:09 +090072#else // Not MSVC
mmentovai@google.comfa5f9932008-08-22 07:26:06 +090073
74#define MSVC_SUPPRESS_WARNING(n)
75#define MSVC_PUSH_DISABLE_WARNING(n)
evanm@google.com5756c4f2008-09-17 04:28:06 +090076#define MSVC_PUSH_WARNING_LEVEL(n)
mmentovai@google.comfa5f9932008-08-22 07:26:06 +090077#define MSVC_POP_WARNING()
deanm@chromium.org520ada62008-11-22 03:48:52 +090078#define MSVC_DISABLE_OPTIMIZE()
79#define MSVC_ENABLE_OPTIMIZE()
mmentovai@google.comfa5f9932008-08-22 07:26:06 +090080#define ALLOW_THIS_IN_INITIALIZER_LIST(code) code
rvargas@google.com1ed7f9a2011-05-19 03:29:36 +090081#define NON_EXPORTED_BASE(code) code
mmentovai@google.comfa5f9932008-08-22 07:26:06 +090082
83#endif // COMPILER_MSVC
84
license.botf003cfe2008-08-24 09:55:55 +090085
evan@chromium.org6b68db32010-10-09 01:20:32 +090086// 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.org19583012008-11-22 08:58:09 +090091#if defined(COMPILER_GCC)
phajdan.jr@chromium.org81427ab2008-12-18 03:30:28 +090092#define ALLOW_UNUSED __attribute__((unused))
davemoore@chromium.orgf895b372011-05-10 05:11:01 +090093#define NOINLINE __attribute__((noinline))
evan@chromium.org6b68db32010-10-09 01:20:32 +090094#else
95#define ALLOW_UNUSED
davemoore@chromium.orgf895b372011-05-10 05:11:01 +090096#define NOINLINE
evan@chromium.org6b68db32010-10-09 01:20:32 +090097#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.org54ad9022011-02-08 02:09:03 +0900105#elif defined(__clang__)
106#define OVERRIDE override
evan@chromium.org6b68db32010-10-09 01:20:32 +0900107#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.org0da7dbd2011-05-20 09:48:35 +0900114// To explicitly ignore a result, see |ignore_result()| in <base/basictypes.h>.
evan@chromium.org6b68db32010-10-09 01:20:32 +0900115#if defined(COMPILER_GCC)
agl@chromium.org19583012008-11-22 08:58:09 +0900116#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
evan@chromium.org6b68db32010-10-09 01:20:32 +0900117#else
118#define WARN_UNUSED_RESULT
119#endif
evan@chromium.org3f6b2602009-11-20 15:53:28 +0900120
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.org6b68db32010-10-09 01:20:32 +0900126#if defined(COMPILER_GCC)
evan@chromium.org3f6b2602009-11-20 15:53:28 +0900127#define PRINTF_FORMAT(format_param, dots_param) \
128 __attribute__((format(printf, format_param, dots_param)))
evan@chromium.org6b68db32010-10-09 01:20:32 +0900129#else
130#define PRINTF_FORMAT(format_param, dots_param)
131#endif
evan@chromium.org3f6b2602009-11-20 15:53:28 +0900132
133// WPRINTF_FORMAT is the same, but for wide format strings.
evan@chromium.org6b68db32010-10-09 01:20:32 +0900134// This doesn't appear to yet be implemented in any compiler.
evan@chromium.org3f6b2602009-11-20 15:53:28 +0900135// 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.org19583012008-11-22 08:58:09 +0900140#endif // BASE_COMPILER_SPECIFIC_H_