Fix emulator build error
[platform/framework/web/chromium-efl.git] / base / compiler_specific.h
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
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) && !defined(__clang__)
11 #error "Only clang-cl is supported on Windows, see https://crbug.com/988071"
12 #endif
13
14 // 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
27 // A wrapper around `__has_attribute`, similar to HAS_CPP_ATTRIBUTE.
28 #if defined(__has_attribute)
29 #define HAS_ATTRIBUTE(x) __has_attribute(x)
30 #else
31 #define HAS_ATTRIBUTE(x) 0
32 #endif
33
34 // A wrapper around `__has_builtin`, similar to HAS_CPP_ATTRIBUTE.
35 #if defined(__has_builtin)
36 #define HAS_BUILTIN(x) __has_builtin(x)
37 #else
38 #define HAS_BUILTIN(x) 0
39 #endif
40
41 // Annotate a function indicating it should not be inlined.
42 // Use like:
43 //   NOINLINE void DoStuff() { ... }
44 #if defined(__clang__) && HAS_ATTRIBUTE(noinline) && !BUILDFLAG(IS_TIZEN)
45 #define NOINLINE [[clang::noinline]]
46 #elif defined(COMPILER_GCC) && HAS_ATTRIBUTE(noinline)
47 #define NOINLINE __attribute__((noinline))
48 #elif defined(COMPILER_MSVC)
49 #define NOINLINE __declspec(noinline)
50 #else
51 #define NOINLINE
52 #endif
53
54 #if defined(__clang__) && defined(NDEBUG) && HAS_ATTRIBUTE(always_inline) && \
55     !BUILDFLAG(IS_TIZEN)
56 #define ALWAYS_INLINE [[clang::always_inline]] inline
57 #elif defined(COMPILER_GCC) && defined(NDEBUG) && HAS_ATTRIBUTE(always_inline)
58 #define ALWAYS_INLINE inline __attribute__((__always_inline__))
59 #elif defined(COMPILER_MSVC) && defined(NDEBUG)
60 #define ALWAYS_INLINE __forceinline
61 #else
62 #define ALWAYS_INLINE inline
63 #endif
64
65 // Annotate a function indicating it should never be tail called. Useful to make
66 // sure callers of the annotated function are never omitted from call-stacks.
67 // To provide the complementary behavior (prevent the annotated function from
68 // being omitted) look at NOINLINE. Also note that this doesn't prevent code
69 // folding of multiple identical caller functions into a single signature. To
70 // prevent code folding, see NO_CODE_FOLDING() in base/debug/alias.h.
71 // Use like:
72 //   NOT_TAIL_CALLED void FooBar();
73 #if defined(__clang__) && HAS_ATTRIBUTE(not_tail_called) && !BUILDFLAG(IS_TIZEN)
74 #define NOT_TAIL_CALLED [[clang::not_tail_called]]
75 #else
76 #define NOT_TAIL_CALLED
77 #endif
78
79 // Specify memory alignment for structs, classes, etc.
80 // Use like:
81 //   class ALIGNAS(16) MyClass { ... }
82 //   ALIGNAS(16) int array[4];
83 //
84 // In most places you can use the C++11 keyword "alignas", which is preferred.
85 //
86 // Historically, compilers had trouble mixing __attribute__((...)) syntax with
87 // alignas(...) syntax. However, at least Clang is very accepting nowadays. It
88 // may be that this macro can be removed entirely.
89 #if defined(__clang__)
90 #define ALIGNAS(byte_alignment) alignas(byte_alignment)
91 #elif defined(COMPILER_MSVC)
92 #define ALIGNAS(byte_alignment) __declspec(align(byte_alignment))
93 #elif defined(COMPILER_GCC) && HAS_ATTRIBUTE(aligned)
94 #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
95 #endif
96
97 // In case the compiler supports it NO_UNIQUE_ADDRESS evaluates to the C++20
98 // attribute [[no_unique_address]]. This allows annotating data members so that
99 // they need not have an address distinct from all other non-static data members
100 // of its class.
101 //
102 // References:
103 // * https://en.cppreference.com/w/cpp/language/attributes/no_unique_address
104 // * https://wg21.link/dcl.attr.nouniqueaddr
105 #if HAS_CPP_ATTRIBUTE(no_unique_address)
106 #define NO_UNIQUE_ADDRESS [[no_unique_address]]
107 #else
108 #define NO_UNIQUE_ADDRESS
109 #endif
110
111 // Tells the compiler a function is using a printf-style format string.
112 // |format_param| is the one-based index of the format string parameter;
113 // |dots_param| is the one-based index of the "..." parameter.
114 // For v*printf functions (which take a va_list), pass 0 for dots_param.
115 // (This is undocumented but matches what the system C headers do.)
116 // For member functions, the implicit this parameter counts as index 1.
117 #if (defined(COMPILER_GCC) || defined(__clang__)) && HAS_ATTRIBUTE(format)
118 #define PRINTF_FORMAT(format_param, dots_param) \
119   __attribute__((format(printf, format_param, dots_param)))
120 #else
121 #define PRINTF_FORMAT(format_param, dots_param)
122 #endif
123
124 // WPRINTF_FORMAT is the same, but for wide format strings.
125 // This doesn't appear to yet be implemented in any compiler.
126 // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 .
127 #define WPRINTF_FORMAT(format_param, dots_param)
128 // If available, it would look like:
129 //   __attribute__((format(wprintf, format_param, dots_param)))
130
131 // Sanitizers annotations.
132 #if HAS_ATTRIBUTE(no_sanitize)
133 #define NO_SANITIZE(what) __attribute__((no_sanitize(what)))
134 #endif
135 #if !defined(NO_SANITIZE)
136 #define NO_SANITIZE(what)
137 #endif
138
139 // MemorySanitizer annotations.
140 #if defined(MEMORY_SANITIZER) && !BUILDFLAG(IS_NACL)
141 #include <sanitizer/msan_interface.h>
142
143 // Mark a memory region fully initialized.
144 // Use this to annotate code that deliberately reads uninitialized data, for
145 // example a GC scavenging root set pointers from the stack.
146 #define MSAN_UNPOISON(p, size) __msan_unpoison(p, size)
147
148 // Check a memory region for initializedness, as if it was being used here.
149 // If any bits are uninitialized, crash with an MSan report.
150 // Use this to sanitize data which MSan won't be able to track, e.g. before
151 // passing data to another process via shared memory.
152 #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) \
153   __msan_check_mem_is_initialized(p, size)
154 #else  // MEMORY_SANITIZER
155 #define MSAN_UNPOISON(p, size)
156 #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size)
157 #endif  // MEMORY_SANITIZER
158
159 // DISABLE_CFI_PERF -- Disable Control Flow Integrity for perf reasons.
160 #if !defined(DISABLE_CFI_PERF)
161 #if defined(__clang__) && defined(OFFICIAL_BUILD)
162 #define DISABLE_CFI_PERF NO_SANITIZE("cfi")
163 #else
164 #define DISABLE_CFI_PERF
165 #endif
166 #endif
167
168 // DISABLE_CFI_ICALL -- Disable Control Flow Integrity indirect call checks.
169 // Security Note: if you just need to allow calling of dlsym functions use
170 // DISABLE_CFI_DLSYM.
171 #if !defined(DISABLE_CFI_ICALL)
172 #if BUILDFLAG(IS_WIN)
173 // Windows also needs __declspec(guard(nocf)).
174 #define DISABLE_CFI_ICALL NO_SANITIZE("cfi-icall") __declspec(guard(nocf))
175 #else
176 #define DISABLE_CFI_ICALL NO_SANITIZE("cfi-icall")
177 #endif
178 #endif
179 #if !defined(DISABLE_CFI_ICALL)
180 #define DISABLE_CFI_ICALL
181 #endif
182
183 // DISABLE_CFI_DLSYM -- applies DISABLE_CFI_ICALL on platforms where dlsym
184 // functions must be called. Retains CFI checks on platforms where loaded
185 // modules participate in CFI (e.g. Windows).
186 #if !defined(DISABLE_CFI_DLSYM)
187 #if BUILDFLAG(IS_WIN)
188 // Windows modules register functions when loaded so can be checked by CFG.
189 #define DISABLE_CFI_DLSYM
190 #else
191 #define DISABLE_CFI_DLSYM DISABLE_CFI_ICALL
192 #endif
193 #endif
194 #if !defined(DISABLE_CFI_DLSYM)
195 #define DISABLE_CFI_DLSYM
196 #endif
197
198 // Macro useful for writing cross-platform function pointers.
199 #if !defined(CDECL)
200 #if BUILDFLAG(IS_WIN)
201 #define CDECL __cdecl
202 #else  // BUILDFLAG(IS_WIN)
203 #define CDECL
204 #endif  // BUILDFLAG(IS_WIN)
205 #endif  // !defined(CDECL)
206
207 // Macro for hinting that an expression is likely to be false.
208 #if !defined(UNLIKELY)
209 #if defined(COMPILER_GCC) || defined(__clang__)
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
216 #if !defined(LIKELY)
217 #if defined(COMPILER_GCC) || defined(__clang__)
218 #define LIKELY(x) __builtin_expect(!!(x), 1)
219 #else
220 #define LIKELY(x) (x)
221 #endif  // defined(COMPILER_GCC)
222 #endif  // !defined(LIKELY)
223
224 // Compiler feature-detection.
225 // 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
230 #endif
231
232 #if defined(COMPILER_GCC)
233 #define PRETTY_FUNCTION __PRETTY_FUNCTION__
234 #elif defined(COMPILER_MSVC)
235 #define PRETTY_FUNCTION __FUNCSIG__
236 #else
237 // See https://en.cppreference.com/w/c/language/function_definition#func
238 #define PRETTY_FUNCTION __func__
239 #endif
240
241 #if !defined(CPU_ARM_NEON)
242 #if defined(__arm__)
243 #if !defined(__ARMEB__) && !defined(__ARM_EABI__) && !defined(__EABI__) && \
244     !defined(__VFP_FP__) && !defined(_WIN32_WCE) && !defined(ANDROID)
245 #error Chromium does not support middle endian architecture
246 #endif
247 #if defined(__ARM_NEON__)
248 #define CPU_ARM_NEON 1
249 #endif
250 #endif  // defined(__arm__)
251 #endif  // !defined(CPU_ARM_NEON)
252
253 #if !defined(HAVE_MIPS_MSA_INTRINSICS)
254 #if defined(__mips_msa) && defined(__mips_isa_rev) && (__mips_isa_rev >= 5)
255 #define HAVE_MIPS_MSA_INTRINSICS 1
256 #endif
257 #endif
258
259 #if defined(__clang__) && HAS_ATTRIBUTE(uninitialized)
260 // Attribute "uninitialized" disables -ftrivial-auto-var-init=pattern for
261 // the specified variable.
262 // Library-wide alternative is
263 // 'configs -= [ "//build/config/compiler:default_init_stack_vars" ]' in .gn
264 // file.
265 //
266 // See "init_stack_vars" in build/config/compiler/BUILD.gn and
267 // http://crbug.com/977230
268 // "init_stack_vars" is enabled for non-official builds and we hope to enable it
269 // in official build in 2020 as well. The flag writes fixed pattern into
270 // uninitialized parts of all local variables. In rare cases such initialization
271 // is undesirable and attribute can be used:
272 //   1. Degraded performance
273 // In most cases compiler is able to remove additional stores. E.g. if memory is
274 // never accessed or properly initialized later. Preserved stores mostly will
275 // not affect program performance. However if compiler failed on some
276 // performance critical code we can get a visible regression in a benchmark.
277 //   2. memset, memcpy calls
278 // Compiler may replaces some memory writes with memset or memcpy calls. This is
279 // not -ftrivial-auto-var-init specific, but it can happen more likely with the
280 // flag. It can be a problem if code is not linked with C run-time library.
281 //
282 // Note: The flag is security risk mitigation feature. So in future the
283 // attribute uses should be avoided when possible. However to enable this
284 // mitigation on the most of the code we need to be less strict now and minimize
285 // number of exceptions later. So if in doubt feel free to use attribute, but
286 // please document the problem for someone who is going to cleanup it later.
287 // E.g. platform, bot, benchmark or test name in patch description or next to
288 // the attribute.
289 #define STACK_UNINITIALIZED [[clang::uninitialized]]
290 #else
291 #define STACK_UNINITIALIZED
292 #endif
293
294 // Attribute "no_stack_protector" disables -fstack-protector for the specified
295 // function.
296 //
297 // "stack_protector" is enabled on most POSIX builds. The flag adds a canary
298 // to each stack frame, which on function return is checked against a reference
299 // canary. If the canaries do not match, it's likely that a stack buffer
300 // overflow has occurred, so immediately crashing will prevent exploitation in
301 // many cases.
302 //
303 // In some cases it's desirable to remove this, e.g. on hot functions, or if
304 // we have purposely changed the reference canary.
305 #if defined(COMPILER_GCC) || defined(__clang__)
306 #if HAS_ATTRIBUTE(__no_stack_protector__)
307 #define NO_STACK_PROTECTOR __attribute__((__no_stack_protector__))
308 #else
309 #define NO_STACK_PROTECTOR __attribute__((__optimize__("-fno-stack-protector")))
310 #endif
311 #else
312 #define NO_STACK_PROTECTOR
313 #endif
314
315 // The ANALYZER_ASSUME_TRUE(bool arg) macro adds compiler-specific hints
316 // to Clang which control what code paths are statically analyzed,
317 // and is meant to be used in conjunction with assert & assert-like functions.
318 // The expression is passed straight through if analysis isn't enabled.
319 //
320 // ANALYZER_SKIP_THIS_PATH() suppresses static analysis for the current
321 // codepath and any other branching codepaths that might follow.
322 #if defined(__clang_analyzer__)
323
324 inline constexpr bool AnalyzerNoReturn() __attribute__((analyzer_noreturn)) {
325   return false;
326 }
327
328 inline constexpr bool AnalyzerAssumeTrue(bool arg) {
329   // AnalyzerNoReturn() is invoked and analysis is terminated if |arg| is
330   // false.
331   return arg || AnalyzerNoReturn();
332 }
333
334 #define ANALYZER_ASSUME_TRUE(arg) ::AnalyzerAssumeTrue(!!(arg))
335 #define ANALYZER_SKIP_THIS_PATH() static_cast<void>(::AnalyzerNoReturn())
336
337 #else  // !defined(__clang_analyzer__)
338
339 #define ANALYZER_ASSUME_TRUE(arg) (arg)
340 #define ANALYZER_SKIP_THIS_PATH()
341
342 #endif  // defined(__clang_analyzer__)
343
344 // Use nomerge attribute to disable optimization of merging multiple same calls.
345 #if defined(__clang__) && HAS_ATTRIBUTE(nomerge) && !BUILDFLAG(IS_TIZEN)
346 #define NOMERGE [[clang::nomerge]]
347 #else
348 #define NOMERGE
349 #endif
350
351 // Marks a type as being eligible for the "trivial" ABI despite having a
352 // non-trivial destructor or copy/move constructor. Such types can be relocated
353 // after construction by simply copying their memory, which makes them eligible
354 // to be passed in registers. The canonical example is std::unique_ptr.
355 //
356 // Use with caution; this has some subtle effects on constructor/destructor
357 // ordering and will be very incorrect if the type relies on its address
358 // remaining constant. When used as a function argument (by value), the value
359 // may be constructed in the caller's stack frame, passed in a register, and
360 // then used and destructed in the callee's stack frame. A similar thing can
361 // occur when values are returned.
362 //
363 // TRIVIAL_ABI is not needed for types which have a trivial destructor and
364 // copy/move constructors, such as base::TimeTicks and other POD.
365 //
366 // It is also not likely to be effective on types too large to be passed in one
367 // or two registers on typical target ABIs.
368 //
369 // See also:
370 //   https://clang.llvm.org/docs/AttributeReference.html#trivial-abi
371 //   https://libcxx.llvm.org/docs/DesignDocs/UniquePtrTrivialAbi.html
372 #if defined(__clang__) && HAS_ATTRIBUTE(trivial_abi)
373 #define TRIVIAL_ABI [[clang::trivial_abi]]
374 #else
375 #define TRIVIAL_ABI
376 #endif
377
378 // Detect whether a type is trivially relocatable, ie. a move-and-destroy
379 // sequence can replaced with memmove(). This can be used to optimise the
380 // implementation of containers. This is automatically true for types that were
381 // defined with TRIVIAL_ABI such as scoped_refptr.
382 //
383 // See also:
384 //   https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p1144r8.html
385 //   https://clang.llvm.org/docs/LanguageExtensions.html#:~:text=__is_trivially_relocatable
386 #if defined(__clang__) && HAS_BUILTIN(__is_trivially_relocatable)
387 #define IS_TRIVIALLY_RELOCATABLE(t) __is_trivially_relocatable(t)
388 #else
389 #define IS_TRIVIALLY_RELOCATABLE(t) false
390 #endif
391
392 // Marks a member function as reinitializing a moved-from variable.
393 // See also
394 // https://clang.llvm.org/extra/clang-tidy/checks/bugprone-use-after-move.html#reinitialization
395 #if defined(__clang__) && HAS_ATTRIBUTE(reinitializes)
396 #define REINITIALIZES_AFTER_MOVE [[clang::reinitializes]]
397 #else
398 #define REINITIALIZES_AFTER_MOVE
399 #endif
400
401 // Requires constant initialization. See constinit in C++20. Allows to rely on a
402 // variable being initialized before execution, and not requiring a global
403 // constructor.
404 #if HAS_ATTRIBUTE(require_constant_initialization)
405 #define CONSTINIT __attribute__((require_constant_initialization))
406 #endif
407 #if !defined(CONSTINIT)
408 #define CONSTINIT
409 #endif
410
411 #if defined(__clang__)
412 #define GSL_OWNER [[gsl::Owner]]
413 #define GSL_POINTER [[gsl::Pointer]]
414 #else
415 #define GSL_OWNER
416 #define GSL_POINTER
417 #endif
418
419 // Adds the "logically_const" tag to a symbol's mangled name. The "Mutable
420 // Constants" check [1] detects instances of constants that aren't in .rodata,
421 // e.g. due to a missing `const`. Using this tag suppresses the check for this
422 // symbol, allowing it to live outside .rodata without a warning.
423 //
424 // [1]:
425 // https://crsrc.org/c/docs/speed/binary_size/android_binary_size_trybot.md#Mutable-Constants
426 #if defined(COMPILER_GCC) || defined(__clang__)
427 #define LOGICALLY_CONST [[gnu::abi_tag("logically_const")]]
428 #else
429 #define LOGICALLY_CONST
430 #endif
431
432 // preserve_most clang's calling convention. Reduces register pressure for the
433 // caller and as such can be used for cold calls. Support for the
434 // "preserve_most" attribute is limited:
435 // - 32-bit platforms do not implement it,
436 // - component builds fail because _dl_runtime_resolve() clobbers registers,
437 // - there are crashes on arm64 on Windows (https://crbug.com/v8/14065), which
438 //   can hopefully be fixed in the future.
439 // Additionally, the initial implementation in clang <= 16 overwrote the return
440 // register(s) in the epilogue of a preserve_most function, so we only use
441 // preserve_most in clang >= 17 (see https://reviews.llvm.org/D143425).
442 // See https://clang.llvm.org/docs/AttributeReference.html#preserve-most for
443 // more details.
444 #if defined(ARCH_CPU_64_BITS) &&                       \
445     !(BUILDFLAG(IS_WIN) && defined(ARCH_CPU_ARM64)) && \
446     !defined(COMPONENT_BUILD) && defined(__clang__) && \
447     __clang_major__ >= 17 && HAS_ATTRIBUTE(preserve_most)
448 #define PRESERVE_MOST __attribute__((preserve_most))
449 #else
450 #define PRESERVE_MOST
451 #endif
452
453 #endif  // BASE_COMPILER_SPECIFIC_H_