xref: /aosp_15_r20/external/pdfium/third_party/base/compiler_specific.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1*3ac0a46fSAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*3ac0a46fSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*3ac0a46fSAndroid Build Coastguard Worker // found in the LICENSE file.
4*3ac0a46fSAndroid Build Coastguard Worker 
5*3ac0a46fSAndroid Build Coastguard Worker #ifndef THIRD_PARTY_BASE_COMPILER_SPECIFIC_H_
6*3ac0a46fSAndroid Build Coastguard Worker #define THIRD_PARTY_BASE_COMPILER_SPECIFIC_H_
7*3ac0a46fSAndroid Build Coastguard Worker 
8*3ac0a46fSAndroid Build Coastguard Worker #include "build/build_config.h"
9*3ac0a46fSAndroid Build Coastguard Worker 
10*3ac0a46fSAndroid Build Coastguard Worker // A wrapper around `__has_attribute`, similar to HAS_CPP_ATTRIBUTE.
11*3ac0a46fSAndroid Build Coastguard Worker #if defined(__has_attribute)
12*3ac0a46fSAndroid Build Coastguard Worker #define HAS_ATTRIBUTE(x) __has_attribute(x)
13*3ac0a46fSAndroid Build Coastguard Worker #else
14*3ac0a46fSAndroid Build Coastguard Worker #define HAS_ATTRIBUTE(x) 0
15*3ac0a46fSAndroid Build Coastguard Worker #endif
16*3ac0a46fSAndroid Build Coastguard Worker 
17*3ac0a46fSAndroid Build Coastguard Worker // Annotate a function indicating it should not be inlined.
18*3ac0a46fSAndroid Build Coastguard Worker // Use like:
19*3ac0a46fSAndroid Build Coastguard Worker //   NOINLINE void DoStuff() { ... }
20*3ac0a46fSAndroid Build Coastguard Worker #if defined(__clang__) && HAS_ATTRIBUTE(noinline)
21*3ac0a46fSAndroid Build Coastguard Worker #define NOINLINE [[clang::noinline]]
22*3ac0a46fSAndroid Build Coastguard Worker #elif defined(COMPILER_GCC) && HAS_ATTRIBUTE(noinline)
23*3ac0a46fSAndroid Build Coastguard Worker #define NOINLINE __attribute__((noinline))
24*3ac0a46fSAndroid Build Coastguard Worker #elif defined(COMPILER_MSVC)
25*3ac0a46fSAndroid Build Coastguard Worker #define NOINLINE __declspec(noinline)
26*3ac0a46fSAndroid Build Coastguard Worker #else
27*3ac0a46fSAndroid Build Coastguard Worker #define NOINLINE
28*3ac0a46fSAndroid Build Coastguard Worker #endif
29*3ac0a46fSAndroid Build Coastguard Worker 
30*3ac0a46fSAndroid Build Coastguard Worker // Macro for hinting that an expression is likely to be false.
31*3ac0a46fSAndroid Build Coastguard Worker #if !defined(UNLIKELY)
32*3ac0a46fSAndroid Build Coastguard Worker #if defined(COMPILER_GCC) || defined(__clang__)
33*3ac0a46fSAndroid Build Coastguard Worker #define UNLIKELY(x) __builtin_expect(!!(x), 0)
34*3ac0a46fSAndroid Build Coastguard Worker #else
35*3ac0a46fSAndroid Build Coastguard Worker #define UNLIKELY(x) (x)
36*3ac0a46fSAndroid Build Coastguard Worker #endif  // defined(COMPILER_GCC)
37*3ac0a46fSAndroid Build Coastguard Worker #endif  // !defined(UNLIKELY)
38*3ac0a46fSAndroid Build Coastguard Worker 
39*3ac0a46fSAndroid Build Coastguard Worker #if !defined(LIKELY)
40*3ac0a46fSAndroid Build Coastguard Worker #if defined(COMPILER_GCC) || defined(__clang__)
41*3ac0a46fSAndroid Build Coastguard Worker #define LIKELY(x) __builtin_expect(!!(x), 1)
42*3ac0a46fSAndroid Build Coastguard Worker #else
43*3ac0a46fSAndroid Build Coastguard Worker #define LIKELY(x) (x)
44*3ac0a46fSAndroid Build Coastguard Worker #endif  // defined(COMPILER_GCC)
45*3ac0a46fSAndroid Build Coastguard Worker #endif  // !defined(LIKELY)
46*3ac0a46fSAndroid Build Coastguard Worker 
47*3ac0a46fSAndroid Build Coastguard Worker // Marks a type as being eligible for the "trivial" ABI despite having a
48*3ac0a46fSAndroid Build Coastguard Worker // non-trivial destructor or copy/move constructor. Such types can be relocated
49*3ac0a46fSAndroid Build Coastguard Worker // after construction by simply copying their memory, which makes them eligible
50*3ac0a46fSAndroid Build Coastguard Worker // to be passed in registers. The canonical example is std::unique_ptr.
51*3ac0a46fSAndroid Build Coastguard Worker //
52*3ac0a46fSAndroid Build Coastguard Worker // Use with caution; this has some subtle effects on constructor/destructor
53*3ac0a46fSAndroid Build Coastguard Worker // ordering and will be very incorrect if the type relies on its address
54*3ac0a46fSAndroid Build Coastguard Worker // remaining constant. When used as a function argument (by value), the value
55*3ac0a46fSAndroid Build Coastguard Worker // may be constructed in the caller's stack frame, passed in a register, and
56*3ac0a46fSAndroid Build Coastguard Worker // then used and destructed in the callee's stack frame. A similar thing can
57*3ac0a46fSAndroid Build Coastguard Worker // occur when values are returned.
58*3ac0a46fSAndroid Build Coastguard Worker //
59*3ac0a46fSAndroid Build Coastguard Worker // TRIVIAL_ABI is not needed for types which have a trivial destructor and
60*3ac0a46fSAndroid Build Coastguard Worker // copy/move constructors, such as base::TimeTicks and other POD.
61*3ac0a46fSAndroid Build Coastguard Worker //
62*3ac0a46fSAndroid Build Coastguard Worker // It is also not likely to be effective on types too large to be passed in one
63*3ac0a46fSAndroid Build Coastguard Worker // or two registers on typical target ABIs.
64*3ac0a46fSAndroid Build Coastguard Worker //
65*3ac0a46fSAndroid Build Coastguard Worker // See also:
66*3ac0a46fSAndroid Build Coastguard Worker //   https://clang.llvm.org/docs/AttributeReference.html#trivial-abi
67*3ac0a46fSAndroid Build Coastguard Worker //   https://libcxx.llvm.org/docs/DesignDocs/UniquePtrTrivialAbi.html
68*3ac0a46fSAndroid Build Coastguard Worker #if defined(__clang__) && HAS_ATTRIBUTE(trivial_abi)
69*3ac0a46fSAndroid Build Coastguard Worker #define TRIVIAL_ABI [[clang::trivial_abi]]
70*3ac0a46fSAndroid Build Coastguard Worker #else
71*3ac0a46fSAndroid Build Coastguard Worker #define TRIVIAL_ABI
72*3ac0a46fSAndroid Build Coastguard Worker #endif
73*3ac0a46fSAndroid Build Coastguard Worker 
74*3ac0a46fSAndroid Build Coastguard Worker #if defined(__clang__)
75*3ac0a46fSAndroid Build Coastguard Worker #define GSL_POINTER [[gsl::Pointer]]
76*3ac0a46fSAndroid Build Coastguard Worker #else
77*3ac0a46fSAndroid Build Coastguard Worker #define GSL_POINTER
78*3ac0a46fSAndroid Build Coastguard Worker #endif
79*3ac0a46fSAndroid Build Coastguard Worker 
80*3ac0a46fSAndroid Build Coastguard Worker #endif  // THIRD_PARTY_BASE_COMPILER_SPECIFIC_H_
81