blob: 34b0c2d7a2913f98c9b7faa10cc5ab7d7640b4c0 [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
brucedawson6a865012015-10-20 05:39:0012// For _Printf_format_string_.
13#include <sal.h>
14
[email protected]f1ea2fa2008-08-21 22:26:0615// Macros for suppressing and disabling warnings on MSVC.
16//
17// Warning numbers are enumerated at:
18// http://msdn.microsoft.com/en-us/library/8x5x43k7(VS.80).aspx
19//
20// The warning pragma:
21// http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx
22//
23// Using __pragma instead of #pragma inside macros:
24// http://msdn.microsoft.com/en-us/library/d9x1s805.aspx
25
26// MSVC_SUPPRESS_WARNING disables warning |n| for the remainder of the line and
27// for the next line of the source file.
28#define MSVC_SUPPRESS_WARNING(n) __pragma(warning(suppress:n))
29
30// MSVC_PUSH_DISABLE_WARNING pushes |n| onto a stack of warnings to be disabled.
31// The warning remains disabled until popped by MSVC_POP_WARNING.
32#define MSVC_PUSH_DISABLE_WARNING(n) __pragma(warning(push)) \
33 __pragma(warning(disable:n))
[email protected]b91902d2008-09-16 19:28:0634
35// MSVC_PUSH_WARNING_LEVEL pushes |n| as the global warning level. The level
36// remains in effect until popped by MSVC_POP_WARNING(). Use 0 to disable all
37// warnings.
38#define MSVC_PUSH_WARNING_LEVEL(n) __pragma(warning(push, n))
39
40// Pop effects of innermost MSVC_PUSH_* macro.
[email protected]f1ea2fa2008-08-21 22:26:0641#define MSVC_POP_WARNING() __pragma(warning(pop))
42
[email protected]0eb9f4342008-11-21 18:48:5243#define MSVC_DISABLE_OPTIMIZE() __pragma(optimize("", off))
44#define MSVC_ENABLE_OPTIMIZE() __pragma(optimize("", on))
45
[email protected]13677b82011-05-18 18:29:3646// Allows exporting a class that inherits from a non-exported base class.
47// This uses suppress instead of push/pop because the delimiter after the
48// declaration (either "," or "{") has to be placed before the pop macro.
49//
50// Example usage:
51// class EXPORT_API Foo : NON_EXPORTED_BASE(public Bar) {
52//
53// MSVC Compiler warning C4275:
54// non dll-interface class 'Bar' used as base for dll-interface class 'Foo'.
[email protected]bfa081972012-02-09 06:48:0955// Note that this is intended to be used only when no access to the base class'
56// static data is done through derived classes or inline methods. For more info,
57// see http://msdn.microsoft.com/en-us/library/3tdb471s(VS.80).aspx
[email protected]13677b82011-05-18 18:29:3658#define NON_EXPORTED_BASE(code) MSVC_SUPPRESS_WARNING(4275) \
59 code
60
[email protected]dd9afc0b2008-11-21 23:58:0961#else // Not MSVC
[email protected]f1ea2fa2008-08-21 22:26:0662
brucedawson6a865012015-10-20 05:39:0063#define _Printf_format_string_
[email protected]f1ea2fa2008-08-21 22:26:0664#define MSVC_SUPPRESS_WARNING(n)
65#define MSVC_PUSH_DISABLE_WARNING(n)
[email protected]b91902d2008-09-16 19:28:0666#define MSVC_PUSH_WARNING_LEVEL(n)
[email protected]f1ea2fa2008-08-21 22:26:0667#define MSVC_POP_WARNING()
[email protected]0eb9f4342008-11-21 18:48:5268#define MSVC_DISABLE_OPTIMIZE()
69#define MSVC_ENABLE_OPTIMIZE()
[email protected]13677b82011-05-18 18:29:3670#define NON_EXPORTED_BASE(code) code
[email protected]f1ea2fa2008-08-21 22:26:0671
72#endif // COMPILER_MSVC
73
[email protected]f50595102010-10-08 16:20:3274// Annotate a variable indicating it's ok if the variable is not used.
75// (Typically used to silence a compiler warning when the assignment
76// is important for some other reason.)
77// Use like:
pkasting99867ef2014-10-16 23:49:2478// int x = ...;
79// ALLOW_UNUSED_LOCAL(x);
kmarshalld10bb7f72017-04-26 02:55:4180#define ALLOW_UNUSED_LOCAL(x) (void)x
pkasting99867ef2014-10-16 23:49:2481
82// Annotate a typedef or function indicating it's ok if it's not used.
83// Use like:
84// typedef Foo Bar ALLOW_UNUSED_TYPE;
thakis803f9832015-07-20 18:10:5585#if defined(COMPILER_GCC) || defined(__clang__)
pkasting99867ef2014-10-16 23:49:2486#define ALLOW_UNUSED_TYPE __attribute__((unused))
[email protected]f50595102010-10-08 16:20:3287#else
pkasting99867ef2014-10-16 23:49:2488#define ALLOW_UNUSED_TYPE
[email protected]2149cc622012-02-14 01:12:1289#endif
90
91// Annotate a function indicating it should not be inlined.
92// Use like:
93// NOINLINE void DoStuff() { ... }
94#if defined(COMPILER_GCC)
95#define NOINLINE __attribute__((noinline))
96#elif defined(COMPILER_MSVC)
97#define NOINLINE __declspec(noinline)
98#else
[email protected]50795a02011-05-09 20:11:0199#define NOINLINE
[email protected]f50595102010-10-08 16:20:32100#endif
101
palmer58184a8282016-11-08 19:15:39102#if COMPILER_GCC && defined(NDEBUG)
103#define ALWAYS_INLINE inline __attribute__((__always_inline__))
104#elif COMPILER_MSVC && defined(NDEBUG)
105#define ALWAYS_INLINE __forceinline
106#else
107#define ALWAYS_INLINE inline
108#endif
109
[email protected]cd924d62012-02-23 17:52:20110// Specify memory alignment for structs, classes, etc.
111// Use like:
112// class ALIGNAS(16) MyClass { ... }
113// ALIGNAS(16) int array[4];
114#if defined(COMPILER_MSVC)
115#define ALIGNAS(byte_alignment) __declspec(align(byte_alignment))
116#elif defined(COMPILER_GCC)
117#define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
118#endif
119
thakis3ebecc32015-04-08 19:34:29120// Return the byte alignment of the given type (available at compile time).
[email protected]cd924d62012-02-23 17:52:20121// Use like:
avi9b6f42932015-12-26 22:15:14122// ALIGNOF(int32_t) // this would be 4
[email protected]cd924d62012-02-23 17:52:20123#if defined(COMPILER_MSVC)
thakis3ebecc32015-04-08 19:34:29124#define ALIGNOF(type) __alignof(type)
[email protected]cd924d62012-02-23 17:52:20125#elif defined(COMPILER_GCC)
126#define ALIGNOF(type) __alignof__(type)
127#endif
128
[email protected]f50595102010-10-08 16:20:32129// Annotate a function indicating the caller must examine the return value.
130// Use like:
131// int foo() WARN_UNUSED_RESULT;
toyoshim20986cf2015-07-03 08:38:07132// To explicitly ignore a result, see |ignore_result()| in base/macros.h.
dcheng7764fe92015-10-10 01:17:26133#undef WARN_UNUSED_RESULT
134#if defined(COMPILER_GCC) || defined(__clang__)
[email protected]dd9afc0b2008-11-21 23:58:09135#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
[email protected]f50595102010-10-08 16:20:32136#else
137#define WARN_UNUSED_RESULT
138#endif
[email protected]34b2b002009-11-20 06:53:28139
140// Tell the compiler a function is using a printf-style format string.
141// |format_param| is the one-based index of the format string parameter;
142// |dots_param| is the one-based index of the "..." parameter.
143// For v*printf functions (which take a va_list), pass 0 for dots_param.
144// (This is undocumented but matches what the system C headers do.)
[email protected]f50595102010-10-08 16:20:32145#if defined(COMPILER_GCC)
[email protected]34b2b002009-11-20 06:53:28146#define PRINTF_FORMAT(format_param, dots_param) \
147 __attribute__((format(printf, format_param, dots_param)))
[email protected]f50595102010-10-08 16:20:32148#else
149#define PRINTF_FORMAT(format_param, dots_param)
150#endif
[email protected]34b2b002009-11-20 06:53:28151
152// WPRINTF_FORMAT is the same, but for wide format strings.
[email protected]f50595102010-10-08 16:20:32153// This doesn't appear to yet be implemented in any compiler.
[email protected]34b2b002009-11-20 06:53:28154// See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 .
155#define WPRINTF_FORMAT(format_param, dots_param)
156// If available, it would look like:
157// __attribute__((format(wprintf, format_param, dots_param)))
158
etienneb4e9250a2016-11-18 18:47:53159// Sanitizers annotations.
160#if defined(__has_attribute)
161#if __has_attribute(no_sanitize)
162#define NO_SANITIZE(what) __attribute__((no_sanitize(what)))
163#endif
164#endif
165#if !defined(NO_SANITIZE)
166#define NO_SANITIZE(what)
167#endif
168
[email protected]75086be2013-03-20 21:18:22169// MemorySanitizer annotations.
[email protected]36c2b1372014-02-07 18:54:44170#if defined(MEMORY_SANITIZER) && !defined(OS_NACL)
[email protected]eb82dfb2014-02-03 19:51:17171#include <sanitizer/msan_interface.h>
[email protected]75086be2013-03-20 21:18:22172
173// Mark a memory region fully initialized.
174// Use this to annotate code that deliberately reads uninitialized data, for
175// example a GC scavenging root set pointers from the stack.
thestig1a42b4072015-03-16 22:36:55176#define MSAN_UNPOISON(p, size) __msan_unpoison(p, size)
177
178// Check a memory region for initializedness, as if it was being used here.
179// If any bits are uninitialized, crash with an MSan report.
180// Use this to sanitize data which MSan won't be able to track, e.g. before
181// passing data to another process via shared memory.
182#define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) \
183 __msan_check_mem_is_initialized(p, size)
[email protected]75086be2013-03-20 21:18:22184#else // MEMORY_SANITIZER
thestig1a42b4072015-03-16 22:36:55185#define MSAN_UNPOISON(p, size)
186#define MSAN_CHECK_MEM_IS_INITIALIZED(p, size)
[email protected]75086be2013-03-20 21:18:22187#endif // MEMORY_SANITIZER
188
krasin825ce482016-08-27 11:01:11189// DISABLE_CFI_PERF -- Disable Control Flow Integrity for perf reasons.
190#if !defined(DISABLE_CFI_PERF)
krasin40f7c782016-09-22 19:04:27191#if defined(__clang__) && defined(OFFICIAL_BUILD)
krasin825ce482016-08-27 11:01:11192#define DISABLE_CFI_PERF __attribute__((no_sanitize("cfi")))
193#else
194#define DISABLE_CFI_PERF
195#endif
196#endif
197
[email protected]5a8d4ce2013-12-18 17:42:27198// Macro useful for writing cross-platform function pointers.
199#if !defined(CDECL)
200#if defined(OS_WIN)
201#define CDECL __cdecl
202#else // defined(OS_WIN)
203#define CDECL
204#endif // defined(OS_WIN)
205#endif // !defined(CDECL)
206
[email protected]2bc0c6992014-02-13 16:11:04207// Macro for hinting that an expression is likely to be false.
208#if !defined(UNLIKELY)
209#if defined(COMPILER_GCC)
210#define UNLIKELY(x) __builtin_expect(!!(x), 0)
211#else
212#define UNLIKELY(x) (x)
213#endif // defined(COMPILER_GCC)
214#endif // !defined(UNLIKELY)
215
palmer58184a8282016-11-08 19:15:39216#if !defined(LIKELY)
217#if defined(COMPILER_GCC)
Chris Palmerad4cb83f2016-11-18 20:02:25218#define LIKELY(x) __builtin_expect(!!(x), 1)
palmer58184a8282016-11-08 19:15:39219#else
220#define LIKELY(x) (x)
221#endif // defined(COMPILER_GCC)
222#endif // !defined(LIKELY)
223
jfbd81c1ce2016-04-05 20:50:35224// Compiler feature-detection.
jfba8dc9dd82016-04-06 20:20:31225// clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension
226#if defined(__has_feature)
227#define HAS_FEATURE(FEATURE) __has_feature(FEATURE)
228#else
229#define HAS_FEATURE(FEATURE) 0
jfbd81c1ce2016-04-05 20:50:35230#endif
231
[email protected]dd9afc0b2008-11-21 23:58:09232#endif // BASE_COMPILER_SPECIFIC_H_