blob: a7076487c9a31be279653fd0544f2b76174d63ef [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
Nico Weberfb053cc2020-03-03 13:33:0510#if defined(COMPILER_MSVC) && !defined(__clang__)
Nico Weber59791812019-07-27 04:02:1111#error "Only clang-cl is supported on Windows, see https://crbug.com/988071"
12#endif
13
Jan Wilken Dörrief8d479d2020-11-23 12:21:1314// This is a wrapper around `__has_cpp_attribute`, which can be used to test for
15// the presence of an attribute. In case the compiler does not support this
16// macro it will simply evaluate to 0.
17//
18// References:
19// https://wg21.link/sd6#testing-for-the-presence-of-an-attribute-__has_cpp_attribute
20// https://wg21.link/cpp.cond#:__has_cpp_attribute
21#if defined(__has_cpp_attribute)
22#define HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
23#else
24#define HAS_CPP_ATTRIBUTE(x) 0
25#endif
26
Jann Horn9e4b48552021-03-04 14:34:2727// A wrapper around `__has_builtin`, similar to HAS_CPP_ATTRIBUTE.
28#if defined(__has_builtin)
29#define HAS_BUILTIN(x) __has_builtin(x)
30#else
31#define HAS_BUILTIN(x) 0
32#endif
33
[email protected]f50595102010-10-08 16:20:3234// Annotate a variable indicating it's ok if the variable is not used.
35// (Typically used to silence a compiler warning when the assignment
36// is important for some other reason.)
37// Use like:
pkasting99867ef2014-10-16 23:49:2438// int x = ...;
39// ALLOW_UNUSED_LOCAL(x);
kmarshalld10bb7f72017-04-26 02:55:4140#define ALLOW_UNUSED_LOCAL(x) (void)x
pkasting99867ef2014-10-16 23:49:2441
42// Annotate a typedef or function indicating it's ok if it's not used.
43// Use like:
44// typedef Foo Bar ALLOW_UNUSED_TYPE;
thakis803f9832015-07-20 18:10:5545#if defined(COMPILER_GCC) || defined(__clang__)
pkasting99867ef2014-10-16 23:49:2446#define ALLOW_UNUSED_TYPE __attribute__((unused))
[email protected]f50595102010-10-08 16:20:3247#else
pkasting99867ef2014-10-16 23:49:2448#define ALLOW_UNUSED_TYPE
[email protected]2149cc622012-02-14 01:12:1249#endif
50
51// Annotate a function indicating it should not be inlined.
52// Use like:
53// NOINLINE void DoStuff() { ... }
54#if defined(COMPILER_GCC)
55#define NOINLINE __attribute__((noinline))
56#elif defined(COMPILER_MSVC)
57#define NOINLINE __declspec(noinline)
58#else
[email protected]50795a02011-05-09 20:11:0159#define NOINLINE
[email protected]f50595102010-10-08 16:20:3260#endif
61
Ivan Krasin9c098a0d2018-08-05 03:57:5762#if defined(COMPILER_GCC) && defined(NDEBUG)
palmer58184a8282016-11-08 19:15:3963#define ALWAYS_INLINE inline __attribute__((__always_inline__))
Ivan Krasin9c098a0d2018-08-05 03:57:5764#elif defined(COMPILER_MSVC) && defined(NDEBUG)
palmer58184a8282016-11-08 19:15:3965#define ALWAYS_INLINE __forceinline
66#else
67#define ALWAYS_INLINE inline
68#endif
69
Olivier Li19d89252020-05-13 17:57:5570// Annotate a function indicating it should never be tail called. Useful to make
71// sure callers of the annotated function are never omitted from call-stacks.
72// To provide the complementary behavior (prevent the annotated function from
73// being omitted) look at NOINLINE. Also note that this doesn't prevent code
74// folding of multiple identical caller functions into a single signature. To
Bruce Dawson7915efd2021-01-27 18:07:5875// prevent code folding, see NO_CODE_FOLDING() in base/debug/alias.h.
Olivier Li19d89252020-05-13 17:57:5576// Use like:
77// void NOT_TAIL_CALLED FooBar();
78#if defined(__clang__) && __has_attribute(not_tail_called)
79#define NOT_TAIL_CALLED __attribute__((not_tail_called))
80#else
81#define NOT_TAIL_CALLED
82#endif
83
[email protected]cd924d62012-02-23 17:52:2084// Specify memory alignment for structs, classes, etc.
85// Use like:
86// class ALIGNAS(16) MyClass { ... }
87// ALIGNAS(16) int array[4];
brettw16289b3e2017-06-13 21:58:4088//
89// In most places you can use the C++11 keyword "alignas", which is preferred.
90//
91// But compilers have trouble mixing __attribute__((...)) syntax with
92// alignas(...) syntax.
93//
94// Doesn't work in clang or gcc:
95// struct alignas(16) __attribute__((packed)) S { char c; };
96// Works in clang but not gcc:
97// struct __attribute__((packed)) alignas(16) S2 { char c; };
98// Works in clang and gcc:
99// struct alignas(16) S3 { char c; } __attribute__((packed));
100//
101// There are also some attributes that must be specified *before* a class
102// definition: visibility (used for exporting functions/classes) is one of
103// these attributes. This means that it is not possible to use alignas() with a
104// class that is marked as exported.
[email protected]cd924d62012-02-23 17:52:20105#if defined(COMPILER_MSVC)
106#define ALIGNAS(byte_alignment) __declspec(align(byte_alignment))
107#elif defined(COMPILER_GCC)
108#define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
109#endif
110
[email protected]f50595102010-10-08 16:20:32111// Annotate a function indicating the caller must examine the return value.
112// Use like:
113// int foo() WARN_UNUSED_RESULT;
toyoshim20986cf2015-07-03 08:38:07114// To explicitly ignore a result, see |ignore_result()| in base/macros.h.
dcheng7764fe92015-10-10 01:17:26115#undef WARN_UNUSED_RESULT
116#if defined(COMPILER_GCC) || defined(__clang__)
[email protected]dd9afc0b2008-11-21 23:58:09117#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
[email protected]f50595102010-10-08 16:20:32118#else
119#define WARN_UNUSED_RESULT
120#endif
[email protected]34b2b002009-11-20 06:53:28121
Jan Wilken Dörrief8d479d2020-11-23 12:21:13122// In case the compiler supports it NO_UNIQUE_ADDRESS evaluates to the C++20
123// attribute [[no_unique_address]]. This allows annotating data members so that
124// they need not have an address distinct from all other non-static data members
125// of its class.
126//
127// References:
128// * https://en.cppreference.com/w/cpp/language/attributes/no_unique_address
129// * https://wg21.link/dcl.attr.nouniqueaddr
130#if HAS_CPP_ATTRIBUTE(no_unique_address)
131#define NO_UNIQUE_ADDRESS [[no_unique_address]]
132#else
133#define NO_UNIQUE_ADDRESS
134#endif
135
[email protected]34b2b002009-11-20 06:53:28136// 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) \
Vitaly Buka2b790762019-12-20 21:11:48144 __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.
Vitaly Buka2b790762019-12-20 21:11:48173#define MSAN_UNPOISON(p, size) __msan_unpoison(p, size)
thestig1a42b4072015-03-16 22:36:55174
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) \
Vitaly Buka2b790762019-12-20 21:11:48180 __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
Will Harris9a033b02020-07-11 01:26:54195// DISABLE_CFI_ICALL -- Disable Control Flow Integrity indirect call checks.
196#if !defined(DISABLE_CFI_ICALL)
197#if defined(OS_WIN)
198// Windows also needs __declspec(guard(nocf)).
199#define DISABLE_CFI_ICALL NO_SANITIZE("cfi-icall") __declspec(guard(nocf))
200#else
201#define DISABLE_CFI_ICALL NO_SANITIZE("cfi-icall")
202#endif
203#endif
204#if !defined(DISABLE_CFI_ICALL)
205#define DISABLE_CFI_ICALL
206#endif
207
[email protected]5a8d4ce2013-12-18 17:42:27208// Macro useful for writing cross-platform function pointers.
209#if !defined(CDECL)
210#if defined(OS_WIN)
211#define CDECL __cdecl
212#else // defined(OS_WIN)
213#define CDECL
214#endif // defined(OS_WIN)
215#endif // !defined(CDECL)
216
[email protected]2bc0c6992014-02-13 16:11:04217// Macro for hinting that an expression is likely to be false.
218#if !defined(UNLIKELY)
Vladimir Levin6b777712017-09-09 00:12:05219#if defined(COMPILER_GCC) || defined(__clang__)
[email protected]2bc0c6992014-02-13 16:11:04220#define UNLIKELY(x) __builtin_expect(!!(x), 0)
221#else
222#define UNLIKELY(x) (x)
223#endif // defined(COMPILER_GCC)
224#endif // !defined(UNLIKELY)
225
palmer58184a8282016-11-08 19:15:39226#if !defined(LIKELY)
Vladimir Levin6b777712017-09-09 00:12:05227#if defined(COMPILER_GCC) || defined(__clang__)
Chris Palmerad4cb83f2016-11-18 20:02:25228#define LIKELY(x) __builtin_expect(!!(x), 1)
palmer58184a8282016-11-08 19:15:39229#else
230#define LIKELY(x) (x)
231#endif // defined(COMPILER_GCC)
232#endif // !defined(LIKELY)
233
jfbd81c1ce2016-04-05 20:50:35234// Compiler feature-detection.
jfba8dc9dd82016-04-06 20:20:31235// clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension
236#if defined(__has_feature)
237#define HAS_FEATURE(FEATURE) __has_feature(FEATURE)
238#else
239#define HAS_FEATURE(FEATURE) 0
jfbd81c1ce2016-04-05 20:50:35240#endif
241
Nico Weber0ae88362018-01-25 19:26:02242// Macro for telling -Wimplicit-fallthrough that a fallthrough is intentional.
Nico Weber0ae88362018-01-25 19:26:02243#if defined(__clang__)
244#define FALLTHROUGH [[clang::fallthrough]]
245#else
246#define FALLTHROUGH
247#endif
Nico Weber0ae88362018-01-25 19:26:02248
Alex Clarke23c6cf72018-11-21 13:22:27249#if defined(COMPILER_GCC)
250#define PRETTY_FUNCTION __PRETTY_FUNCTION__
251#elif defined(COMPILER_MSVC)
252#define PRETTY_FUNCTION __FUNCSIG__
253#else
254// See https://en.cppreference.com/w/c/language/function_definition#func
255#define PRETTY_FUNCTION __func__
256#endif
257
Henrique Ferreiro6daf71db2019-04-03 13:12:42258#if !defined(CPU_ARM_NEON)
259#if defined(__arm__)
260#if !defined(__ARMEB__) && !defined(__ARM_EABI__) && !defined(__EABI__) && \
261 !defined(__VFP_FP__) && !defined(_WIN32_WCE) && !defined(ANDROID)
262#error Chromium does not support middle endian architecture
263#endif
264#if defined(__ARM_NEON__)
265#define CPU_ARM_NEON 1
266#endif
267#endif // defined(__arm__)
268#endif // !defined(CPU_ARM_NEON)
269
270#if !defined(HAVE_MIPS_MSA_INTRINSICS)
271#if defined(__mips_msa) && defined(__mips_isa_rev) && (__mips_isa_rev >= 5)
272#define HAVE_MIPS_MSA_INTRINSICS 1
273#endif
274#endif
275
Vitaly Buka2b790762019-12-20 21:11:48276#if defined(__clang__) && __has_attribute(uninitialized)
277// Attribute "uninitialized" disables -ftrivial-auto-var-init=pattern for
278// the specified variable.
279// Library-wide alternative is
280// 'configs -= [ "//build/config/compiler:default_init_stack_vars" ]' in .gn
281// file.
282//
283// See "init_stack_vars" in build/config/compiler/BUILD.gn and
284// http://crbug.com/977230
285// "init_stack_vars" is enabled for non-official builds and we hope to enable it
286// in official build in 2020 as well. The flag writes fixed pattern into
287// uninitialized parts of all local variables. In rare cases such initialization
288// is undesirable and attribute can be used:
289// 1. Degraded performance
290// In most cases compiler is able to remove additional stores. E.g. if memory is
291// never accessed or properly initialized later. Preserved stores mostly will
292// not affect program performance. However if compiler failed on some
293// performance critical code we can get a visible regression in a benchmark.
294// 2. memset, memcpy calls
295// Compiler may replaces some memory writes with memset or memcpy calls. This is
296// not -ftrivial-auto-var-init specific, but it can happen more likely with the
297// flag. It can be a problem if code is not linked with C run-time library.
298//
299// Note: The flag is security risk mitigation feature. So in future the
300// attribute uses should be avoided when possible. However to enable this
301// mitigation on the most of the code we need to be less strict now and minimize
302// number of exceptions later. So if in doubt feel free to use attribute, but
303// please document the problem for someone who is going to cleanup it later.
304// E.g. platform, bot, benchmark or test name in patch description or next to
305// the attribute.
306#define STACK_UNINITIALIZED __attribute__((uninitialized))
307#else
308#define STACK_UNINITIALIZED
309#endif
310
Hans Wennborg12aea3e2020-04-14 15:29:00311// The ANALYZER_ASSUME_TRUE(bool arg) macro adds compiler-specific hints
312// to Clang which control what code paths are statically analyzed,
313// and is meant to be used in conjunction with assert & assert-like functions.
314// The expression is passed straight through if analysis isn't enabled.
315//
316// ANALYZER_SKIP_THIS_PATH() suppresses static analysis for the current
317// codepath and any other branching codepaths that might follow.
318#if defined(__clang_analyzer__)
319
320inline constexpr bool AnalyzerNoReturn() __attribute__((analyzer_noreturn)) {
321 return false;
322}
323
324inline constexpr bool AnalyzerAssumeTrue(bool arg) {
325 // AnalyzerNoReturn() is invoked and analysis is terminated if |arg| is
326 // false.
327 return arg || AnalyzerNoReturn();
328}
329
George Burgess IVa09d235d2020-04-17 13:32:50330#define ANALYZER_ASSUME_TRUE(arg) ::AnalyzerAssumeTrue(!!(arg))
331#define ANALYZER_SKIP_THIS_PATH() static_cast<void>(::AnalyzerNoReturn())
Hans Wennborg12aea3e2020-04-14 15:29:00332#define ANALYZER_ALLOW_UNUSED(var) static_cast<void>(var);
333
334#else // !defined(__clang_analyzer__)
335
336#define ANALYZER_ASSUME_TRUE(arg) (arg)
337#define ANALYZER_SKIP_THIS_PATH()
338#define ANALYZER_ALLOW_UNUSED(var) static_cast<void>(var);
339
340#endif // defined(__clang_analyzer__)
341
Zequan Wu9909f142021-02-10 03:26:00342// Use nomerge attribute to disable optimization of merging multiple same calls.
Manoj Guptadb4c8852021-02-26 13:48:04343#if defined(__clang__) && __has_attribute(nomerge)
Zequan Wu9909f142021-02-10 03:26:00344#define NOMERGE [[clang::nomerge]]
345#else
346#define NOMERGE
347#endif
348
Jeremy Roman810d98d2021-04-06 16:46:07349// Marks a type as being eligible for the "trivial" ABI despite having a
350// non-trivial destructor or copy/move constructor. Such types can be relocated
351// after construction by simply copying their memory, which makes them eligible
352// to be passed in registers. The canonical example is std::unique_ptr.
353//
354// Use with caution; this has some subtle effects on constructor/destructor
355// ordering and will be very incorrect if the type relies on its address
356// remaining constant. When used as a function argument (by value), the value
357// may be constructed in the caller's stack frame, passed in a register, and
358// then used and destructed in the callee's stack frame. A similar thing can
359// occur when values are returned.
360//
361// TRIVIAL_ABI is not needed for types which have a trivial destructor and
362// copy/move constructors, such as base::TimeTicks and other POD.
363//
364// It is also not likely to be effective on types too large to be passed in one
365// or two registers on typical target ABIs.
366//
367// See also:
368// https://clang.llvm.org/docs/AttributeReference.html#trivial-abi
369// https://libcxx.llvm.org/docs/DesignDocs/UniquePtrTrivialAbi.html
370#if defined(__clang__) && __has_attribute(trivial_abi)
371#define TRIVIAL_ABI [[clang::trivial_abi]]
372#else
373#define TRIVIAL_ABI
374#endif
375
Lukasz Anforowicz3be38fbb2021-04-14 20:29:29376// Marks a member function as reinitializing a moved-from variable.
377// See also
378// https://clang.llvm.org/extra/clang-tidy/checks/bugprone-use-after-move.html#reinitialization
379#if defined(__clang__) && __has_attribute(reinitializes)
380#define REINITIALIZES_AFTER_MOVE [[clang::reinitializes]]
381#else
382#define REINITIALIZES_AFTER_MOVE
383#endif
384
[email protected]dd9afc0b2008-11-21 23:58:09385#endif // BASE_COMPILER_SPECIFIC_H_