1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef INCLUDE_PERFETTO_BASE_COMPILER_H_
18 #define INCLUDE_PERFETTO_BASE_COMPILER_H_
19
20 #include <stddef.h>
21 #include <type_traits>
22
23 #include "perfetto/base/build_config.h"
24
25 // __has_attribute is supported only by clang and recent versions of GCC.
26 // Add a layer to wrap the __has_attribute macro.
27 #if defined(__has_attribute)
28 #define PERFETTO_HAS_ATTRIBUTE(x) __has_attribute(x)
29 #else
30 #define PERFETTO_HAS_ATTRIBUTE(x) 0
31 #endif
32
33 #if defined(__GNUC__) || defined(__clang__)
34 #define PERFETTO_LIKELY(_x) __builtin_expect(!!(_x), 1)
35 #define PERFETTO_UNLIKELY(_x) __builtin_expect(!!(_x), 0)
36 #else
37 #define PERFETTO_LIKELY(_x) (_x)
38 #define PERFETTO_UNLIKELY(_x) (_x)
39 #endif
40
41 #if defined(__GNUC__) || defined(__clang__)
42 #define PERFETTO_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
43 #else
44 #define PERFETTO_WARN_UNUSED_RESULT
45 #endif
46
47 #if defined(__clang__)
48 #define PERFETTO_ALWAYS_INLINE __attribute__((__always_inline__))
49 #define PERFETTO_NO_INLINE __attribute__((__noinline__))
50 #else
51 // GCC is too pedantic and often fails with the error:
52 // "always_inline function might not be inlinable"
53 #define PERFETTO_ALWAYS_INLINE
54 #define PERFETTO_NO_INLINE
55 #endif
56
57 #if defined(__GNUC__) || defined(__clang__)
58 #define PERFETTO_NORETURN __attribute__((__noreturn__))
59 #else
60 #define PERFETTO_NORETURN __declspec(noreturn)
61 #endif
62
63 #if defined(__GNUC__) || defined(__clang__)
64 #define PERFETTO_DEBUG_FUNCTION_IDENTIFIER() __PRETTY_FUNCTION__
65 #elif defined(_MSC_VER)
66 #define PERFETTO_DEBUG_FUNCTION_IDENTIFIER() __FUNCSIG__
67 #else
68 #define PERFETTO_DEBUG_FUNCTION_IDENTIFIER() \
69 static_assert(false, "Not implemented for this compiler")
70 #endif
71
72 #if defined(__GNUC__) || defined(__clang__)
73 #define PERFETTO_PRINTF_FORMAT(x, y) \
74 __attribute__((__format__(__printf__, x, y)))
75 #else
76 #define PERFETTO_PRINTF_FORMAT(x, y)
77 #endif
78
79 #if PERFETTO_BUILDFLAG(PERFETTO_OS_IOS)
80 // TODO(b/158814068): For iOS builds, thread_local is only supported since iOS
81 // 8. We'd have to use pthread for thread local data instead here. For now, just
82 // define it to nothing since we don't support running perfetto or the client
83 // lib on iOS right now.
84 #define PERFETTO_THREAD_LOCAL
85 #else
86 #define PERFETTO_THREAD_LOCAL thread_local
87 #endif
88
89 #if defined(__GNUC__) || defined(__clang__)
90 #define PERFETTO_POPCOUNT(x) __builtin_popcountll(x)
91 #else
92 #include <intrin.h>
93 #define PERFETTO_POPCOUNT(x) __popcnt64(x)
94 #endif
95
96 #if defined(__clang__)
97 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
98 extern "C" void __asan_poison_memory_region(void const volatile*, size_t);
99 extern "C" void __asan_unpoison_memory_region(void const volatile*, size_t);
100 #define PERFETTO_ASAN_POISON(a, s) __asan_poison_memory_region((a), (s))
101 #define PERFETTO_ASAN_UNPOISON(a, s) __asan_unpoison_memory_region((a), (s))
102 #else
103 #define PERFETTO_ASAN_POISON(addr, size)
104 #define PERFETTO_ASAN_UNPOISON(addr, size)
105 #endif // __has_feature(address_sanitizer)
106 #else
107 #define PERFETTO_ASAN_POISON(addr, size)
108 #define PERFETTO_ASAN_UNPOISON(addr, size)
109 #endif // __clang__
110
111 #if defined(__GNUC__) || defined(__clang__)
112 #define PERFETTO_IS_LITTLE_ENDIAN() __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
113 #else
114 // Assume all MSVC targets are little endian.
115 #define PERFETTO_IS_LITTLE_ENDIAN() 1
116 #endif
117
118 // This is used for exporting xxxMain() symbols (e.g., PerfettoCmdMain,
119 // ProbesMain) from libperfetto.so when the GN arg monolithic_binaries = false.
120 #if defined(__GNUC__) || defined(__clang__)
121 #define PERFETTO_EXPORT_ENTRYPOINT __attribute__((visibility("default")))
122 #else
123 // TODO(primiano): on Windows this should be a pair of dllexport/dllimport. But
124 // that requires a -DXXX_IMPLEMENTATION depending on whether we are on the
125 // impl-site or call-site. Right now it's not worth the trouble as we
126 // force-export the xxxMain() symbols only on Android, where we pack all the
127 // code for N binaries into one .so to save binary size. On Windows we support
128 // only monolithic binaries, as they are easier to deal with.
129 #define PERFETTO_EXPORT_ENTRYPOINT
130 #endif
131
132 // Disables thread safety analysis for functions where the compiler can't
133 // accurate figure out which locks are being held.
134 #if defined(__clang__)
135 #define PERFETTO_NO_THREAD_SAFETY_ANALYSIS \
136 __attribute__((no_thread_safety_analysis))
137 #else
138 #define PERFETTO_NO_THREAD_SAFETY_ANALYSIS
139 #endif
140
141 // Avoid calling the exit-time destructor on an object with static lifetime.
142 #if PERFETTO_HAS_ATTRIBUTE(no_destroy)
143 #define PERFETTO_HAS_NO_DESTROY() 1
144 #define PERFETTO_NO_DESTROY __attribute__((no_destroy))
145 #else
146 #define PERFETTO_HAS_NO_DESTROY() 0
147 #define PERFETTO_NO_DESTROY
148 #endif
149
150 namespace perfetto {
151 namespace base {
152
153 template <typename... T>
ignore_result(const T &...)154 inline void ignore_result(const T&...) {}
155
156 } // namespace base
157 } // namespace perfetto
158
159 #endif // INCLUDE_PERFETTO_BASE_COMPILER_H_
160