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