blob: 2ba34da1cc4bbfef27264de7b34ece19628ef1b0 [file] [log] [blame]
[email protected]2149cc622012-02-14 01:12:121// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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]13677b82011-05-18 18:29:3643// 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'.
[email protected]bfa081972012-02-09 06:48:0952// 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
[email protected]13677b82011-05-18 18:29:3655#define NON_EXPORTED_BASE(code) MSVC_SUPPRESS_WARNING(4275) \
56 code
57
[email protected]dd9afc0b2008-11-21 23:58:0958#else // Not MSVC
[email protected]f1ea2fa2008-08-21 22:26:0659
60#define MSVC_SUPPRESS_WARNING(n)
61#define MSVC_PUSH_DISABLE_WARNING(n)
[email protected]b91902d2008-09-16 19:28:0662#define MSVC_PUSH_WARNING_LEVEL(n)
[email protected]f1ea2fa2008-08-21 22:26:0663#define MSVC_POP_WARNING()
[email protected]0eb9f4342008-11-21 18:48:5264#define MSVC_DISABLE_OPTIMIZE()
65#define MSVC_ENABLE_OPTIMIZE()
[email protected]13677b82011-05-18 18:29:3666#define NON_EXPORTED_BASE(code) code
[email protected]f1ea2fa2008-08-21 22:26:0667
68#endif // COMPILER_MSVC
69
license.botbf09a502008-08-24 00:55:5570
[email protected]ccf8453d2013-11-07 17:49:5071// 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
[email protected]f50595102010-10-08 16:20:3293// 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 = ...;
[email protected]dd9afc0b2008-11-21 23:58:0998#if defined(COMPILER_GCC)
[email protected]85e0f1f2008-12-17 18:30:2899#define ALLOW_UNUSED __attribute__((unused))
[email protected]f50595102010-10-08 16:20:32100#else
101#define ALLOW_UNUSED
[email protected]2149cc622012-02-14 01:12:12102#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
[email protected]50795a02011-05-09 20:11:01112#define NOINLINE
[email protected]f50595102010-10-08 16:20:32113#endif
114
[email protected]cd924d62012-02-23 17:52:20115// 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
[email protected]9f01b022012-07-26 02:22:39125// 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
[email protected]cd924d62012-02-23 17:52:20128// Use like:
129// ALIGNOF(int32) // this would be 4
130#if defined(COMPILER_MSVC)
[email protected]9f01b022012-07-26 02:22:39131#define ALIGNOF(type) (sizeof(type) - sizeof(type) + __alignof(type))
[email protected]cd924d62012-02-23 17:52:20132#elif defined(COMPILER_GCC)
133#define ALIGNOF(type) __alignof__(type)
134#endif
135
[email protected]f50595102010-10-08 16:20:32136// 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;
140#if defined(COMPILER_MSVC)
141#define OVERRIDE override
[email protected]ad67f972011-02-07 17:09:03142#elif defined(__clang__)
143#define OVERRIDE override
[email protected]9d6985f62013-12-06 14:16:22144#elif defined(COMPILER_GCC) && __cplusplus >= 201103 && \
145 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40700
146// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
147#define OVERRIDE override
[email protected]f50595102010-10-08 16:20:32148#else
149#define OVERRIDE
150#endif
151
[email protected]3122c372013-05-10 22:28:16152// Annotate a virtual method indicating that subclasses must not override it,
153// or annotate a class to indicate that it cannot be subclassed.
154// Use like:
155// virtual void foo() FINAL;
156// class B FINAL : public A {};
[email protected]389501382014-02-03 16:10:51157#if defined(__clang__)
158#define FINAL final
159#elif defined(COMPILER_MSVC)
[email protected]3122c372013-05-10 22:28:16160// TODO(jered): Change this to "final" when chromium no longer uses MSVC 2010.
161#define FINAL sealed
[email protected]9d6985f62013-12-06 14:16:22162#elif defined(COMPILER_GCC) && __cplusplus >= 201103 && \
163 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40700
164// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
165#define FINAL final
[email protected]3122c372013-05-10 22:28:16166#else
167#define FINAL
168#endif
169
[email protected]f50595102010-10-08 16:20:32170// Annotate a function indicating the caller must examine the return value.
171// Use like:
172// int foo() WARN_UNUSED_RESULT;
[email protected]117bbe32011-05-20 00:48:35173// To explicitly ignore a result, see |ignore_result()| in <base/basictypes.h>.
[email protected]f50595102010-10-08 16:20:32174#if defined(COMPILER_GCC)
[email protected]dd9afc0b2008-11-21 23:58:09175#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
[email protected]f50595102010-10-08 16:20:32176#else
177#define WARN_UNUSED_RESULT
178#endif
[email protected]34b2b002009-11-20 06:53:28179
180// Tell the compiler a function is using a printf-style format string.
181// |format_param| is the one-based index of the format string parameter;
182// |dots_param| is the one-based index of the "..." parameter.
183// For v*printf functions (which take a va_list), pass 0 for dots_param.
184// (This is undocumented but matches what the system C headers do.)
[email protected]f50595102010-10-08 16:20:32185#if defined(COMPILER_GCC)
[email protected]34b2b002009-11-20 06:53:28186#define PRINTF_FORMAT(format_param, dots_param) \
187 __attribute__((format(printf, format_param, dots_param)))
[email protected]f50595102010-10-08 16:20:32188#else
189#define PRINTF_FORMAT(format_param, dots_param)
190#endif
[email protected]34b2b002009-11-20 06:53:28191
192// WPRINTF_FORMAT is the same, but for wide format strings.
[email protected]f50595102010-10-08 16:20:32193// This doesn't appear to yet be implemented in any compiler.
[email protected]34b2b002009-11-20 06:53:28194// See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 .
195#define WPRINTF_FORMAT(format_param, dots_param)
196// If available, it would look like:
197// __attribute__((format(wprintf, format_param, dots_param)))
198
[email protected]75086be2013-03-20 21:18:22199
200// MemorySanitizer annotations.
[email protected]36c2b1372014-02-07 18:54:44201#if defined(MEMORY_SANITIZER) && !defined(OS_NACL)
[email protected]eb82dfb2014-02-03 19:51:17202#include <sanitizer/msan_interface.h>
[email protected]75086be2013-03-20 21:18:22203
204// Mark a memory region fully initialized.
205// Use this to annotate code that deliberately reads uninitialized data, for
206// example a GC scavenging root set pointers from the stack.
207#define MSAN_UNPOISON(p, s) __msan_unpoison(p, s)
208#else // MEMORY_SANITIZER
209#define MSAN_UNPOISON(p, s)
210#endif // MEMORY_SANITIZER
211
[email protected]5a8d4ce2013-12-18 17:42:27212// Macro useful for writing cross-platform function pointers.
213#if !defined(CDECL)
214#if defined(OS_WIN)
215#define CDECL __cdecl
216#else // defined(OS_WIN)
217#define CDECL
218#endif // defined(OS_WIN)
219#endif // !defined(CDECL)
220
[email protected]dd9afc0b2008-11-21 23:58:09221#endif // BASE_COMPILER_SPECIFIC_H_