blob: 9e2111db6b6e1aea9aa59beb96047c3c3ef2c3eb [file] [log] [blame]
eroman@chromium.orgf1ab5fe2012-02-14 10:12:12 +09001// Copyright (c) 2012 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_
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.com5756c4f2008-09-17 04:28:06 +090031
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.comfa5f9932008-08-22 07:26:06 +090038#define MSVC_POP_WARNING() __pragma(warning(pop))
39
deanm@chromium.org520ada62008-11-22 03:48:52 +090040#define MSVC_DISABLE_OPTIMIZE() __pragma(optimize("", off))
41#define MSVC_ENABLE_OPTIMIZE() __pragma(optimize("", on))
42
rvargas@google.com1ed7f9a2011-05-19 03:29:36 +090043// Allows exporting a class that inherits from a non-exported base class.
44// This uses suppress instead of push/pop because the delimiter after the
45// declaration (either "," or "{") has to be placed before the pop macro.
46//
47// Example usage:
48// class EXPORT_API Foo : NON_EXPORTED_BASE(public Bar) {
49//
50// MSVC Compiler warning C4275:
51// non dll-interface class 'Bar' used as base for dll-interface class 'Foo'.
jam@chromium.org743e2e32012-02-09 15:48:09 +090052// Note that this is intended to be used only when no access to the base class'
53// static data is done through derived classes or inline methods. For more info,
54// see http://msdn.microsoft.com/en-us/library/3tdb471s(VS.80).aspx
rvargas@google.com1ed7f9a2011-05-19 03:29:36 +090055#define NON_EXPORTED_BASE(code) MSVC_SUPPRESS_WARNING(4275) \
56 code
57
agl@chromium.org19583012008-11-22 08:58:09 +090058#else // Not MSVC
mmentovai@google.comfa5f9932008-08-22 07:26:06 +090059
60#define MSVC_SUPPRESS_WARNING(n)
61#define MSVC_PUSH_DISABLE_WARNING(n)
evanm@google.com5756c4f2008-09-17 04:28:06 +090062#define MSVC_PUSH_WARNING_LEVEL(n)
mmentovai@google.comfa5f9932008-08-22 07:26:06 +090063#define MSVC_POP_WARNING()
deanm@chromium.org520ada62008-11-22 03:48:52 +090064#define MSVC_DISABLE_OPTIMIZE()
65#define MSVC_ENABLE_OPTIMIZE()
rvargas@google.com1ed7f9a2011-05-19 03:29:36 +090066#define NON_EXPORTED_BASE(code) code
mmentovai@google.comfa5f9932008-08-22 07:26:06 +090067
68#endif // COMPILER_MSVC
69
license.botf003cfe2008-08-24 09:55:55 +090070
viettrungluu@chromium.org7b35b882013-11-08 02:49:50 +090071// The C++ standard requires that static const members have an out-of-class
72// definition (in a single compilation unit), but MSVC chokes on this (when
73// language extensions, which are required, are enabled). (You're only likely to
74// notice the need for a definition if you take the address of the member or,
75// more commonly, pass it to a function that takes it as a reference argument --
76// probably an STL function.) This macro makes MSVC do the right thing. See
77// http://msdn.microsoft.com/en-us/library/34h23df8(v=vs.100).aspx for more
78// information. Use like:
79//
80// In .h file:
81// struct Foo {
82// static const int kBar = 5;
83// };
84//
85// In .cc file:
86// STATIC_CONST_MEMBER_DEFINITION const int Foo::kBar;
87#if defined(COMPILER_MSVC)
88#define STATIC_CONST_MEMBER_DEFINITION __declspec(selectany)
89#else
90#define STATIC_CONST_MEMBER_DEFINITION
91#endif
92
evan@chromium.org6b68db32010-10-09 01:20:32 +090093// Annotate a variable indicating it's ok if the variable is not used.
94// (Typically used to silence a compiler warning when the assignment
95// is important for some other reason.)
96// Use like:
97// int x ALLOW_UNUSED = ...;
agl@chromium.org19583012008-11-22 08:58:09 +090098#if defined(COMPILER_GCC)
phajdan.jr@chromium.org81427ab2008-12-18 03:30:28 +090099#define ALLOW_UNUSED __attribute__((unused))
evan@chromium.org6b68db32010-10-09 01:20:32 +0900100#else
101#define ALLOW_UNUSED
eroman@chromium.orgf1ab5fe2012-02-14 10:12:12 +0900102#endif
103
104// Annotate a function indicating it should not be inlined.
105// Use like:
106// NOINLINE void DoStuff() { ... }
107#if defined(COMPILER_GCC)
108#define NOINLINE __attribute__((noinline))
109#elif defined(COMPILER_MSVC)
110#define NOINLINE __declspec(noinline)
111#else
davemoore@chromium.orgf895b372011-05-10 05:11:01 +0900112#define NOINLINE
evan@chromium.org6b68db32010-10-09 01:20:32 +0900113#endif
114
jbates@chromium.orgdde8e302012-02-24 02:52:20 +0900115// Specify memory alignment for structs, classes, etc.
116// Use like:
117// class ALIGNAS(16) MyClass { ... }
118// ALIGNAS(16) int array[4];
119#if defined(COMPILER_MSVC)
120#define ALIGNAS(byte_alignment) __declspec(align(byte_alignment))
121#elif defined(COMPILER_GCC)
122#define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
123#endif
124
dalecurtis@chromium.orgee816432012-07-26 11:22:39 +0900125// Return the byte alignment of the given type (available at compile time). Use
126// sizeof(type) prior to checking __alignof to workaround Visual C++ bug:
127// http://goo.gl/isH0C
jbates@chromium.orgdde8e302012-02-24 02:52:20 +0900128// Use like:
129// ALIGNOF(int32) // this would be 4
130#if defined(COMPILER_MSVC)
dalecurtis@chromium.orgee816432012-07-26 11:22:39 +0900131#define ALIGNOF(type) (sizeof(type) - sizeof(type) + __alignof(type))
jbates@chromium.orgdde8e302012-02-24 02:52:20 +0900132#elif defined(COMPILER_GCC)
133#define ALIGNOF(type) __alignof__(type)
134#endif
135
evan@chromium.org6b68db32010-10-09 01:20:32 +0900136// Annotate a virtual method indicating it must be overriding a virtual
137// method in the parent class.
138// Use like:
139// virtual void foo() OVERRIDE;
thakis@chromium.orgddbf7292014-04-08 02:28:14 +0900140#if defined(__clang__) || defined(COMPILER_MSVC)
thakis@chromium.org54ad9022011-02-08 02:09:03 +0900141#define OVERRIDE override
ch.dumez@samsung.com7d932ba2013-12-06 23:16:22 +0900142#elif defined(COMPILER_GCC) && __cplusplus >= 201103 && \
143 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40700
144// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
145#define OVERRIDE override
evan@chromium.org6b68db32010-10-09 01:20:32 +0900146#else
147#define OVERRIDE
148#endif
149
jered@chromium.org1a0e3142013-05-11 07:28:16 +0900150// Annotate a virtual method indicating that subclasses must not override it,
151// or annotate a class to indicate that it cannot be subclassed.
152// Use like:
153// virtual void foo() FINAL;
154// class B FINAL : public A {};
thakis@chromium.orgddbf7292014-04-08 02:28:14 +0900155#if defined(__clang__) || defined(COMPILER_MSVC)
hans@chromium.org4f7a2b72014-02-04 01:10:51 +0900156#define FINAL final
ch.dumez@samsung.com7d932ba2013-12-06 23:16:22 +0900157#elif defined(COMPILER_GCC) && __cplusplus >= 201103 && \
158 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40700
159// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
160#define FINAL final
jered@chromium.org1a0e3142013-05-11 07:28:16 +0900161#else
162#define FINAL
163#endif
164
evan@chromium.org6b68db32010-10-09 01:20:32 +0900165// Annotate a function indicating the caller must examine the return value.
166// Use like:
167// int foo() WARN_UNUSED_RESULT;
shess@chromium.org0da7dbd2011-05-20 09:48:35 +0900168// To explicitly ignore a result, see |ignore_result()| in <base/basictypes.h>.
evan@chromium.org6b68db32010-10-09 01:20:32 +0900169#if defined(COMPILER_GCC)
agl@chromium.org19583012008-11-22 08:58:09 +0900170#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
evan@chromium.org6b68db32010-10-09 01:20:32 +0900171#else
172#define WARN_UNUSED_RESULT
173#endif
evan@chromium.org3f6b2602009-11-20 15:53:28 +0900174
175// Tell the compiler a function is using a printf-style format string.
176// |format_param| is the one-based index of the format string parameter;
177// |dots_param| is the one-based index of the "..." parameter.
178// For v*printf functions (which take a va_list), pass 0 for dots_param.
179// (This is undocumented but matches what the system C headers do.)
evan@chromium.org6b68db32010-10-09 01:20:32 +0900180#if defined(COMPILER_GCC)
evan@chromium.org3f6b2602009-11-20 15:53:28 +0900181#define PRINTF_FORMAT(format_param, dots_param) \
182 __attribute__((format(printf, format_param, dots_param)))
evan@chromium.org6b68db32010-10-09 01:20:32 +0900183#else
184#define PRINTF_FORMAT(format_param, dots_param)
185#endif
evan@chromium.org3f6b2602009-11-20 15:53:28 +0900186
187// WPRINTF_FORMAT is the same, but for wide format strings.
evan@chromium.org6b68db32010-10-09 01:20:32 +0900188// This doesn't appear to yet be implemented in any compiler.
evan@chromium.org3f6b2602009-11-20 15:53:28 +0900189// See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 .
190#define WPRINTF_FORMAT(format_param, dots_param)
191// If available, it would look like:
192// __attribute__((format(wprintf, format_param, dots_param)))
193
eugenis@chromium.org5eff8312013-03-21 06:18:22 +0900194// MemorySanitizer annotations.
earthdok@chromium.org815eb602014-02-08 03:54:44 +0900195#if defined(MEMORY_SANITIZER) && !defined(OS_NACL)
earthdok@chromium.org0446a672014-02-04 04:51:17 +0900196#include <sanitizer/msan_interface.h>
eugenis@chromium.org5eff8312013-03-21 06:18:22 +0900197
198// Mark a memory region fully initialized.
199// Use this to annotate code that deliberately reads uninitialized data, for
200// example a GC scavenging root set pointers from the stack.
201#define MSAN_UNPOISON(p, s) __msan_unpoison(p, s)
202#else // MEMORY_SANITIZER
203#define MSAN_UNPOISON(p, s)
204#endif // MEMORY_SANITIZER
205
jochen@chromium.org74e19e22013-12-19 02:42:27 +0900206// Macro useful for writing cross-platform function pointers.
207#if !defined(CDECL)
208#if defined(OS_WIN)
209#define CDECL __cdecl
210#else // defined(OS_WIN)
211#define CDECL
212#endif // defined(OS_WIN)
213#endif // !defined(CDECL)
214
skyostil@chromium.org98e93c02014-02-14 01:11:04 +0900215// Macro for hinting that an expression is likely to be false.
216#if !defined(UNLIKELY)
217#if defined(COMPILER_GCC)
218#define UNLIKELY(x) __builtin_expect(!!(x), 0)
219#else
220#define UNLIKELY(x) (x)
221#endif // defined(COMPILER_GCC)
222#endif // !defined(UNLIKELY)
223
agl@chromium.org19583012008-11-22 08:58:09 +0900224#endif // BASE_COMPILER_SPECIFIC_H_