blob: 7e2c5101b5483779ce0370bdbeda3980620ec744 [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
Nico Weber59791812019-07-27 04:02:1112#if !defined(__clang__)
13#error "Only clang-cl is supported on Windows, see https://crbug.com/988071"
14#endif
15
[email protected]f1ea2fa2008-08-21 22:26:0616// Macros for suppressing and disabling warnings on MSVC.
17//
18// Warning numbers are enumerated at:
19// http://msdn.microsoft.com/en-us/library/8x5x43k7(VS.80).aspx
20//
21// The warning pragma:
22// http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx
23//
24// Using __pragma instead of #pragma inside macros:
25// http://msdn.microsoft.com/en-us/library/d9x1s805.aspx
26
[email protected]f1ea2fa2008-08-21 22:26:0627// 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
[email protected]b91902d2008-09-16 19:28:0632// Pop effects of innermost MSVC_PUSH_* macro.
[email protected]f1ea2fa2008-08-21 22:26:0633#define MSVC_POP_WARNING() __pragma(warning(pop))
34
[email protected]dd9afc0b2008-11-21 23:58:0935#else // Not MSVC
[email protected]f1ea2fa2008-08-21 22:26:0636
[email protected]f1ea2fa2008-08-21 22:26:0637#define MSVC_PUSH_DISABLE_WARNING(n)
38#define MSVC_POP_WARNING()
[email protected]0eb9f4342008-11-21 18:48:5239#define MSVC_DISABLE_OPTIMIZE()
40#define MSVC_ENABLE_OPTIMIZE()
[email protected]f1ea2fa2008-08-21 22:26:0641
42#endif // COMPILER_MSVC
43
Bruce Dawsonf141f572019-01-10 19:06:3644// These macros can be helpful when investigating compiler bugs or when
45// investigating issues in local optimized builds, by temporarily disabling
46// optimizations for a single function or file. These macros should never be
47// used to permanently work around compiler bugs or other mysteries, and should
48// not be used in landed changes.
49#if !defined(OFFICIAL_BUILD)
50#if defined(__clang__)
51#define DISABLE_OPTIMIZE() __pragma(clang optimize off)
52#define ENABLE_OPTIMIZE() __pragma(clang optimize on)
53#elif defined(COMPILER_MSVC)
54#define DISABLE_OPTIMIZE() __pragma(optimize("", off))
55#define ENABLE_OPTIMIZE() __pragma(optimize("", on))
56#else
57// These macros are not currently available for other compiler options.
58#endif
59// These macros are not available in official builds.
60#endif // !defined(OFFICIAL_BUILD)
61
[email protected]f50595102010-10-08 16:20:3262// Annotate a variable indicating it's ok if the variable is not used.
63// (Typically used to silence a compiler warning when the assignment
64// is important for some other reason.)
65// Use like:
pkasting99867ef2014-10-16 23:49:2466// int x = ...;
67// ALLOW_UNUSED_LOCAL(x);
kmarshalld10bb7f72017-04-26 02:55:4168#define ALLOW_UNUSED_LOCAL(x) (void)x
pkasting99867ef2014-10-16 23:49:2469
70// Annotate a typedef or function indicating it's ok if it's not used.
71// Use like:
72// typedef Foo Bar ALLOW_UNUSED_TYPE;
thakis803f9832015-07-20 18:10:5573#if defined(COMPILER_GCC) || defined(__clang__)
pkasting99867ef2014-10-16 23:49:2474#define ALLOW_UNUSED_TYPE __attribute__((unused))
[email protected]f50595102010-10-08 16:20:3275#else
pkasting99867ef2014-10-16 23:49:2476#define ALLOW_UNUSED_TYPE
[email protected]2149cc622012-02-14 01:12:1277#endif
78
79// Annotate a function indicating it should not be inlined.
80// Use like:
81// NOINLINE void DoStuff() { ... }
82#if defined(COMPILER_GCC)
83#define NOINLINE __attribute__((noinline))
84#elif defined(COMPILER_MSVC)
85#define NOINLINE __declspec(noinline)
86#else
[email protected]50795a02011-05-09 20:11:0187#define NOINLINE
[email protected]f50595102010-10-08 16:20:3288#endif
89
Ivan Krasin9c098a0d2018-08-05 03:57:5790#if defined(COMPILER_GCC) && defined(NDEBUG)
palmer58184a8282016-11-08 19:15:3991#define ALWAYS_INLINE inline __attribute__((__always_inline__))
Ivan Krasin9c098a0d2018-08-05 03:57:5792#elif defined(COMPILER_MSVC) && defined(NDEBUG)
palmer58184a8282016-11-08 19:15:3993#define ALWAYS_INLINE __forceinline
94#else
95#define ALWAYS_INLINE inline
96#endif
97
[email protected]cd924d62012-02-23 17:52:2098// Specify memory alignment for structs, classes, etc.
99// Use like:
100// class ALIGNAS(16) MyClass { ... }
101// ALIGNAS(16) int array[4];
brettw16289b3e2017-06-13 21:58:40102//
103// In most places you can use the C++11 keyword "alignas", which is preferred.
104//
105// But compilers have trouble mixing __attribute__((...)) syntax with
106// alignas(...) syntax.
107//
108// Doesn't work in clang or gcc:
109// struct alignas(16) __attribute__((packed)) S { char c; };
110// Works in clang but not gcc:
111// struct __attribute__((packed)) alignas(16) S2 { char c; };
112// Works in clang and gcc:
113// struct alignas(16) S3 { char c; } __attribute__((packed));
114//
115// There are also some attributes that must be specified *before* a class
116// definition: visibility (used for exporting functions/classes) is one of
117// these attributes. This means that it is not possible to use alignas() with a
118// class that is marked as exported.
[email protected]cd924d62012-02-23 17:52:20119#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]f50595102010-10-08 16:20:32125// Annotate a function indicating the caller must examine the return value.
126// Use like:
127// int foo() WARN_UNUSED_RESULT;
toyoshim20986cf2015-07-03 08:38:07128// To explicitly ignore a result, see |ignore_result()| in base/macros.h.
dcheng7764fe92015-10-10 01:17:26129#undef WARN_UNUSED_RESULT
130#if defined(COMPILER_GCC) || defined(__clang__)
[email protected]dd9afc0b2008-11-21 23:58:09131#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
[email protected]f50595102010-10-08 16:20:32132#else
133#define WARN_UNUSED_RESULT
134#endif
[email protected]34b2b002009-11-20 06:53:28135
136// Tell the compiler a function is using a printf-style format string.
137// |format_param| is the one-based index of the format string parameter;
138// |dots_param| is the one-based index of the "..." parameter.
139// For v*printf functions (which take a va_list), pass 0 for dots_param.
140// (This is undocumented but matches what the system C headers do.)
Nico Weberfc7c8dd2019-02-28 21:28:44141// For member functions, the implicit this parameter counts as index 1.
Bruce Dawson73528d12017-08-09 01:04:29142#if defined(COMPILER_GCC) || defined(__clang__)
[email protected]34b2b002009-11-20 06:53:28143#define PRINTF_FORMAT(format_param, dots_param) \
144 __attribute__((format(printf, format_param, dots_param)))
[email protected]f50595102010-10-08 16:20:32145#else
146#define PRINTF_FORMAT(format_param, dots_param)
147#endif
[email protected]34b2b002009-11-20 06:53:28148
149// WPRINTF_FORMAT is the same, but for wide format strings.
[email protected]f50595102010-10-08 16:20:32150// This doesn't appear to yet be implemented in any compiler.
[email protected]34b2b002009-11-20 06:53:28151// See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 .
152#define WPRINTF_FORMAT(format_param, dots_param)
153// If available, it would look like:
154// __attribute__((format(wprintf, format_param, dots_param)))
155
etienneb4e9250a2016-11-18 18:47:53156// Sanitizers annotations.
157#if defined(__has_attribute)
158#if __has_attribute(no_sanitize)
159#define NO_SANITIZE(what) __attribute__((no_sanitize(what)))
160#endif
161#endif
162#if !defined(NO_SANITIZE)
163#define NO_SANITIZE(what)
164#endif
165
[email protected]75086be2013-03-20 21:18:22166// MemorySanitizer annotations.
[email protected]36c2b1372014-02-07 18:54:44167#if defined(MEMORY_SANITIZER) && !defined(OS_NACL)
[email protected]eb82dfb2014-02-03 19:51:17168#include <sanitizer/msan_interface.h>
[email protected]75086be2013-03-20 21:18:22169
170// Mark a memory region fully initialized.
171// Use this to annotate code that deliberately reads uninitialized data, for
172// example a GC scavenging root set pointers from the stack.
thestig1a42b4072015-03-16 22:36:55173#define MSAN_UNPOISON(p, size) __msan_unpoison(p, size)
174
175// Check a memory region for initializedness, as if it was being used here.
176// If any bits are uninitialized, crash with an MSan report.
177// Use this to sanitize data which MSan won't be able to track, e.g. before
178// passing data to another process via shared memory.
179#define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) \
180 __msan_check_mem_is_initialized(p, size)
[email protected]75086be2013-03-20 21:18:22181#else // MEMORY_SANITIZER
thestig1a42b4072015-03-16 22:36:55182#define MSAN_UNPOISON(p, size)
183#define MSAN_CHECK_MEM_IS_INITIALIZED(p, size)
[email protected]75086be2013-03-20 21:18:22184#endif // MEMORY_SANITIZER
185
krasin825ce482016-08-27 11:01:11186// DISABLE_CFI_PERF -- Disable Control Flow Integrity for perf reasons.
187#if !defined(DISABLE_CFI_PERF)
krasin40f7c782016-09-22 19:04:27188#if defined(__clang__) && defined(OFFICIAL_BUILD)
krasin825ce482016-08-27 11:01:11189#define DISABLE_CFI_PERF __attribute__((no_sanitize("cfi")))
190#else
191#define DISABLE_CFI_PERF
192#endif
193#endif
194
[email protected]5a8d4ce2013-12-18 17:42:27195// Macro useful for writing cross-platform function pointers.
196#if !defined(CDECL)
197#if defined(OS_WIN)
198#define CDECL __cdecl
199#else // defined(OS_WIN)
200#define CDECL
201#endif // defined(OS_WIN)
202#endif // !defined(CDECL)
203
[email protected]2bc0c6992014-02-13 16:11:04204// Macro for hinting that an expression is likely to be false.
205#if !defined(UNLIKELY)
Vladimir Levin6b777712017-09-09 00:12:05206#if defined(COMPILER_GCC) || defined(__clang__)
[email protected]2bc0c6992014-02-13 16:11:04207#define UNLIKELY(x) __builtin_expect(!!(x), 0)
208#else
209#define UNLIKELY(x) (x)
210#endif // defined(COMPILER_GCC)
211#endif // !defined(UNLIKELY)
212
palmer58184a8282016-11-08 19:15:39213#if !defined(LIKELY)
Vladimir Levin6b777712017-09-09 00:12:05214#if defined(COMPILER_GCC) || defined(__clang__)
Chris Palmerad4cb83f2016-11-18 20:02:25215#define LIKELY(x) __builtin_expect(!!(x), 1)
palmer58184a8282016-11-08 19:15:39216#else
217#define LIKELY(x) (x)
218#endif // defined(COMPILER_GCC)
219#endif // !defined(LIKELY)
220
jfbd81c1ce2016-04-05 20:50:35221// Compiler feature-detection.
jfba8dc9dd82016-04-06 20:20:31222// clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension
223#if defined(__has_feature)
224#define HAS_FEATURE(FEATURE) __has_feature(FEATURE)
225#else
226#define HAS_FEATURE(FEATURE) 0
jfbd81c1ce2016-04-05 20:50:35227#endif
228
Nico Weber0ae88362018-01-25 19:26:02229// Macro for telling -Wimplicit-fallthrough that a fallthrough is intentional.
Nico Weber0ae88362018-01-25 19:26:02230#if defined(__clang__)
231#define FALLTHROUGH [[clang::fallthrough]]
232#else
233#define FALLTHROUGH
234#endif
Nico Weber0ae88362018-01-25 19:26:02235
Alex Clarke23c6cf72018-11-21 13:22:27236#if defined(COMPILER_GCC)
237#define PRETTY_FUNCTION __PRETTY_FUNCTION__
238#elif defined(COMPILER_MSVC)
239#define PRETTY_FUNCTION __FUNCSIG__
240#else
241// See https://en.cppreference.com/w/c/language/function_definition#func
242#define PRETTY_FUNCTION __func__
243#endif
244
Henrique Ferreiro6daf71db2019-04-03 13:12:42245#if !defined(CPU_ARM_NEON)
246#if defined(__arm__)
247#if !defined(__ARMEB__) && !defined(__ARM_EABI__) && !defined(__EABI__) && \
248 !defined(__VFP_FP__) && !defined(_WIN32_WCE) && !defined(ANDROID)
249#error Chromium does not support middle endian architecture
250#endif
251#if defined(__ARM_NEON__)
252#define CPU_ARM_NEON 1
253#endif
254#endif // defined(__arm__)
255#endif // !defined(CPU_ARM_NEON)
256
257#if !defined(HAVE_MIPS_MSA_INTRINSICS)
258#if defined(__mips_msa) && defined(__mips_isa_rev) && (__mips_isa_rev >= 5)
259#define HAVE_MIPS_MSA_INTRINSICS 1
260#endif
261#endif
262
[email protected]dd9afc0b2008-11-21 23:58:09263#endif // BASE_COMPILER_SPECIFIC_H_