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 #include "perfetto/public/compiler.h"
25
26 // __has_attribute is supported only by clang and recent versions of GCC.
27 // Add a layer to wrap the __has_attribute macro.
28 #if defined(__has_attribute)
29 #define PERFETTO_HAS_ATTRIBUTE(x) __has_attribute(x)
30 #else
31 #define PERFETTO_HAS_ATTRIBUTE(x) 0
32 #endif
33
34 #if defined(__GNUC__) || defined(__clang__)
35 #define PERFETTO_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
36 #else
37 #define PERFETTO_WARN_UNUSED_RESULT
38 #endif
39
40 #if defined(__GNUC__) || defined(__clang__)
41 #define PERFETTO_UNUSED __attribute__((unused))
42 #else
43 #define PERFETTO_UNUSED
44 #endif
45
46 #if defined(__clang__)
47 #define PERFETTO_ALWAYS_INLINE __attribute__((__always_inline__))
48 #define PERFETTO_NO_INLINE __attribute__((__noinline__))
49 #else
50 // GCC is too pedantic and often fails with the error:
51 // "always_inline function might not be inlinable"
52 #define PERFETTO_ALWAYS_INLINE
53 #define PERFETTO_NO_INLINE
54 #endif
55
56 #if defined(__GNUC__) || defined(__clang__)
57 #define PERFETTO_NORETURN __attribute__((__noreturn__))
58 #else
59 #define PERFETTO_NORETURN __declspec(noreturn)
60 #endif
61
62 #if defined(__GNUC__) || defined(__clang__)
63 #define PERFETTO_DEBUG_FUNCTION_IDENTIFIER() __PRETTY_FUNCTION__
64 #elif defined(_MSC_VER)
65 #define PERFETTO_DEBUG_FUNCTION_IDENTIFIER() __FUNCSIG__
66 #else
67 #define PERFETTO_DEBUG_FUNCTION_IDENTIFIER() \
68 static_assert(false, "Not implemented for this compiler")
69 #endif
70
71 #if defined(__GNUC__) || defined(__clang__)
72 #define PERFETTO_PRINTF_FORMAT(x, y) \
73 __attribute__((__format__(__printf__, x, y)))
74 #else
75 #define PERFETTO_PRINTF_FORMAT(x, y)
76 #endif
77
78 #if PERFETTO_BUILDFLAG(PERFETTO_OS_IOS)
79 // TODO(b/158814068): For iOS builds, thread_local is only supported since iOS
80 // 8. We'd have to use pthread for thread local data instead here. For now, just
81 // define it to nothing since we don't support running perfetto or the client
82 // lib on iOS right now.
83 #define PERFETTO_THREAD_LOCAL
84 #else
85 #define PERFETTO_THREAD_LOCAL thread_local
86 #endif
87
88 #if defined(__GNUC__) || defined(__clang__)
89 #define PERFETTO_POPCOUNT(x) __builtin_popcountll(x)
90 #else
91 #include <intrin.h>
92 #define PERFETTO_POPCOUNT(x) __popcnt64(x)
93 #endif
94
95 #if defined(__clang__)
96 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
97 extern "C" void __asan_poison_memory_region(void const volatile*, size_t);
98 extern "C" void __asan_unpoison_memory_region(void const volatile*, size_t);
99 #define PERFETTO_ASAN_POISON(a, s) __asan_poison_memory_region((a), (s))
100 #define PERFETTO_ASAN_UNPOISON(a, s) __asan_unpoison_memory_region((a), (s))
101 #else
102 #define PERFETTO_ASAN_POISON(addr, size)
103 #define PERFETTO_ASAN_UNPOISON(addr, size)
104 #endif // __has_feature(address_sanitizer)
105 #else
106 #define PERFETTO_ASAN_POISON(addr, size)
107 #define PERFETTO_ASAN_UNPOISON(addr, size)
108 #endif // __clang__
109
110 #if defined(__GNUC__) || defined(__clang__)
111 #define PERFETTO_IS_LITTLE_ENDIAN() __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
112 #else
113 // Assume all MSVC targets are little endian.
114 #define PERFETTO_IS_LITTLE_ENDIAN() 1
115 #endif
116
117 // This is used for exporting xxxMain() symbols (e.g., PerfettoCmdMain,
118 // ProbesMain) from libperfetto.so when the GN arg monolithic_binaries = false.
119 #if defined(__GNUC__) || defined(__clang__)
120 #define PERFETTO_EXPORT_ENTRYPOINT __attribute__((visibility("default")))
121 #else
122 // TODO(primiano): on Windows this should be a pair of dllexport/dllimport. But
123 // that requires a -DXXX_IMPLEMENTATION depending on whether we are on the
124 // impl-site or call-site. Right now it's not worth the trouble as we
125 // force-export the xxxMain() symbols only on Android, where we pack all the
126 // code for N binaries into one .so to save binary size. On Windows we support
127 // only monolithic binaries, as they are easier to deal with.
128 #define PERFETTO_EXPORT_ENTRYPOINT
129 #endif
130
131 // Disables thread safety analysis for functions where the compiler can't
132 // accurate figure out which locks are being held.
133 #if defined(__clang__)
134 #define PERFETTO_NO_THREAD_SAFETY_ANALYSIS \
135 __attribute__((no_thread_safety_analysis))
136 #else
137 #define PERFETTO_NO_THREAD_SAFETY_ANALYSIS
138 #endif
139
140 // Disables undefined behavior analysis for a function.
141 #if defined(__clang__)
142 #define PERFETTO_NO_SANITIZE_UNDEFINED __attribute__((no_sanitize("undefined")))
143 #else
144 #define PERFETTO_NO_SANITIZE_UNDEFINED
145 #endif
146
147 // Avoid calling the exit-time destructor on an object with static lifetime.
148 #if PERFETTO_HAS_ATTRIBUTE(no_destroy)
149 #define PERFETTO_HAS_NO_DESTROY() 1
150 #define PERFETTO_NO_DESTROY __attribute__((no_destroy))
151 #else
152 #define PERFETTO_HAS_NO_DESTROY() 0
153 #define PERFETTO_NO_DESTROY
154 #endif
155
156 // Macro for telling -Wimplicit-fallthrough that a fallthrough is intentional.
157 #if defined(__clang__)
158 #define PERFETTO_FALLTHROUGH [[clang::fallthrough]]
159 #else
160 #define PERFETTO_FALLTHROUGH
161 #endif
162
163 namespace perfetto {
164 namespace base {
165
166 template <typename... T>
ignore_result(const T &...)167 inline void ignore_result(const T&...) {}
168
169 } // namespace base
170 } // namespace perfetto
171
172 #endif // INCLUDE_PERFETTO_BASE_COMPILER_H_
173