• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/public/compiler.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_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
35 #else
36 #define PERFETTO_WARN_UNUSED_RESULT
37 #endif
38 
39 #if defined(__GNUC__) || defined(__clang__)
40 #define PERFETTO_UNUSED __attribute__((unused))
41 #else
42 #define PERFETTO_UNUSED
43 #endif
44 
45 #if defined(__GNUC__) || defined(__clang__)
46 #define PERFETTO_NORETURN __attribute__((__noreturn__))
47 #else
48 #define PERFETTO_NORETURN __declspec(noreturn)
49 #endif
50 
51 #if defined(__GNUC__) || defined(__clang__)
52 #define PERFETTO_DEBUG_FUNCTION_IDENTIFIER() __PRETTY_FUNCTION__
53 #elif defined(_MSC_VER)
54 #define PERFETTO_DEBUG_FUNCTION_IDENTIFIER() __FUNCSIG__
55 #else
56 #define PERFETTO_DEBUG_FUNCTION_IDENTIFIER() \
57   static_assert(false, "Not implemented for this compiler")
58 #endif
59 
60 #if defined(__GNUC__) || defined(__clang__)
61 #define PERFETTO_PRINTF_FORMAT(x, y) \
62   __attribute__((__format__(__printf__, x, y)))
63 #else
64 #define PERFETTO_PRINTF_FORMAT(x, y)
65 #endif
66 
67 #if defined(__GNUC__) || defined(__clang__)
68 #define PERFETTO_POPCOUNT(x) __builtin_popcountll(x)
69 #else
70 #include <intrin.h>
71 #define PERFETTO_POPCOUNT(x) __popcnt64(x)
72 #endif
73 
74 #if defined(__clang__)
75 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
76 extern "C" void __asan_poison_memory_region(void const volatile*, size_t);
77 extern "C" void __asan_unpoison_memory_region(void const volatile*, size_t);
78 #define PERFETTO_ASAN_POISON(a, s) __asan_poison_memory_region((a), (s))
79 #define PERFETTO_ASAN_UNPOISON(a, s) __asan_unpoison_memory_region((a), (s))
80 #else
81 #define PERFETTO_ASAN_POISON(addr, size)
82 #define PERFETTO_ASAN_UNPOISON(addr, size)
83 #endif  // __has_feature(address_sanitizer)
84 #else
85 #define PERFETTO_ASAN_POISON(addr, size)
86 #define PERFETTO_ASAN_UNPOISON(addr, size)
87 #endif  // __clang__
88 
89 #if defined(__GNUC__) || defined(__clang__)
90 #define PERFETTO_IS_LITTLE_ENDIAN() __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
91 #else
92 // Assume all MSVC targets are little endian.
93 #define PERFETTO_IS_LITTLE_ENDIAN() 1
94 #endif
95 
96 // This is used for exporting xxxMain() symbols (e.g., PerfettoCmdMain,
97 // ProbesMain) from libperfetto.so when the GN arg monolithic_binaries = false.
98 #if defined(__GNUC__) || defined(__clang__)
99 #define PERFETTO_EXPORT_ENTRYPOINT __attribute__((visibility("default")))
100 #else
101 // TODO(primiano): on Windows this should be a pair of dllexport/dllimport. But
102 // that requires a -DXXX_IMPLEMENTATION depending on whether we are on the
103 // impl-site or call-site. Right now it's not worth the trouble as we
104 // force-export the xxxMain() symbols only on Android, where we pack all the
105 // code for N binaries into one .so to save binary size. On Windows we support
106 // only monolithic binaries, as they are easier to deal with.
107 #define PERFETTO_EXPORT_ENTRYPOINT
108 #endif
109 
110 // Disables thread safety analysis for functions where the compiler can't
111 // accurate figure out which locks are being held.
112 #if defined(__clang__)
113 #define PERFETTO_NO_THREAD_SAFETY_ANALYSIS \
114   __attribute__((no_thread_safety_analysis))
115 #else
116 #define PERFETTO_NO_THREAD_SAFETY_ANALYSIS
117 #endif
118 
119 // Disables undefined behavior analysis for a function.
120 #if defined(__clang__)
121 #define PERFETTO_NO_SANITIZE_UNDEFINED __attribute__((no_sanitize("undefined")))
122 #else
123 #define PERFETTO_NO_SANITIZE_UNDEFINED
124 #endif
125 
126 // Avoid calling the exit-time destructor on an object with static lifetime.
127 #if PERFETTO_HAS_ATTRIBUTE(no_destroy)
128 #define PERFETTO_HAS_NO_DESTROY() 1
129 #define PERFETTO_NO_DESTROY __attribute__((no_destroy))
130 #else
131 #define PERFETTO_HAS_NO_DESTROY() 0
132 #define PERFETTO_NO_DESTROY
133 #endif
134 
135 // Macro for telling -Wimplicit-fallthrough that a fallthrough is intentional.
136 #define PERFETTO_FALLTHROUGH [[fallthrough]]
137 
138 namespace perfetto {
139 namespace base {
140 
141 template <typename... T>
ignore_result(const T &...)142 inline void ignore_result(const T&...) {}
143 
144 }  // namespace base
145 }  // namespace perfetto
146 
147 #endif  // INCLUDE_PERFETTO_BASE_COMPILER_H_
148