apply clang-tidy modernize-use-override
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2017 Facebook, Inc.
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 #pragma once
18
19 #include <string.h>
20
21 #include <cstddef>
22
23 #include <folly/portability/Config.h>
24
25 #include <folly/CPortability.h>
26
27 // Unaligned loads and stores
28 namespace folly {
29 #if FOLLY_HAVE_UNALIGNED_ACCESS
30 constexpr bool kHasUnalignedAccess = true;
31 #else
32 constexpr bool kHasUnalignedAccess = false;
33 #endif
34 }
35
36 // compiler specific attribute translation
37 // msvc should come first, so if clang is in msvc mode it gets the right defines
38
39 #if defined(__clang__) || defined(__GNUC__)
40 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
41 #elif defined(_MSC_VER)
42 # define FOLLY_ALIGNED(size) __declspec(align(size))
43 #else
44 # error Cannot define FOLLY_ALIGNED on this platform
45 #endif
46 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
47
48 // NOTE: this will only do checking in msvc with versions that support /analyze
49 #if _MSC_VER
50 # ifdef _USE_ATTRIBUTES_FOR_SAL
51 #    undef _USE_ATTRIBUTES_FOR_SAL
52 # endif
53 /* nolint */
54 # define _USE_ATTRIBUTES_FOR_SAL 1
55 # include <sal.h>
56 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
57 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
58 #else
59 # define FOLLY_PRINTF_FORMAT /**/
60 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
61   __attribute__((__format__(__printf__, format_param, dots_param)))
62 #endif
63
64 // deprecated
65 #if defined(__clang__) || defined(__GNUC__)
66 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
67 #elif defined(_MSC_VER)
68 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
69 #else
70 # define FOLLY_DEPRECATED(msg)
71 #endif
72
73 // warn unused result
74 #if defined(__has_cpp_attribute)
75 #if __has_cpp_attribute(nodiscard)
76 #define FOLLY_NODISCARD [[nodiscard]]
77 #endif
78 #endif
79 #if !defined FOLLY_NODISCARD
80 #if defined(_MSC_VER) && (_MSC_VER >= 1700)
81 #define FOLLY_NODISCARD _Check_return_
82 #elif defined(__clang__) || defined(__GNUC__)
83 #define FOLLY_NODISCARD __attribute__((__warn_unused_result__))
84 #else
85 #define FOLLY_NODISCARD
86 #endif
87 #endif
88
89 // target
90 #ifdef _MSC_VER
91 # define FOLLY_TARGET_ATTRIBUTE(target)
92 #else
93 # define FOLLY_TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
94 #endif
95
96 // detection for 64 bit
97 #if defined(__x86_64__) || defined(_M_X64)
98 # define FOLLY_X64 1
99 #else
100 # define FOLLY_X64 0
101 #endif
102
103 #if defined(__aarch64__)
104 # define FOLLY_A64 1
105 #else
106 # define FOLLY_A64 0
107 #endif
108
109 #if defined (__powerpc64__)
110 # define FOLLY_PPC64 1
111 #else
112 # define FOLLY_PPC64 0
113 #endif
114
115 namespace folly {
116 constexpr bool kIsArchAmd64 = FOLLY_X64 == 1;
117 constexpr bool kIsArchAArch64 = FOLLY_A64 == 1;
118 constexpr bool kIsArchPPC64 = FOLLY_PPC64 == 1;
119 }
120
121 namespace folly {
122
123 #if FOLLY_SANITIZE_ADDRESS
124 constexpr bool kIsSanitizeAddress = true;
125 #else
126 constexpr bool kIsSanitizeAddress = false;
127 #endif
128
129 #if FOLLY_SANITIZE_THREAD
130 constexpr bool kIsSanitizeThread = true;
131 #else
132 constexpr bool kIsSanitizeThread = false;
133 #endif
134 }
135
136 // packing is very ugly in msvc
137 #ifdef _MSC_VER
138 # define FOLLY_PACK_ATTR /**/
139 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
140 # define FOLLY_PACK_POP __pragma(pack(pop))
141 #elif defined(__clang__) || defined(__GNUC__)
142 # define FOLLY_PACK_ATTR __attribute__((__packed__))
143 # define FOLLY_PACK_PUSH /**/
144 # define FOLLY_PACK_POP /**/
145 #else
146 # define FOLLY_PACK_ATTR /**/
147 # define FOLLY_PACK_PUSH /**/
148 # define FOLLY_PACK_POP /**/
149 #endif
150
151 // Generalize warning push/pop.
152 #if defined(_MSC_VER)
153 # define FOLLY_PUSH_WARNING __pragma(warning(push))
154 # define FOLLY_POP_WARNING __pragma(warning(pop))
155 // Disable the GCC warnings.
156 # define FOLLY_GCC_DISABLE_WARNING(warningName)
157 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber) __pragma(warning(disable: warningNumber))
158 #elif defined(__clang__) || defined(__GNUC__)
159 # define FOLLY_PUSH_WARNING _Pragma("GCC diagnostic push")
160 # define FOLLY_POP_WARNING _Pragma("GCC diagnostic pop")
161 # define FOLLY_GCC_DISABLE_WARNING_INTERNAL2(warningName) #warningName
162 # define FOLLY_GCC_DISABLE_WARNING(warningName) \
163   _Pragma(                                      \
164   FOLLY_GCC_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored warningName))
165 // Disable the MSVC warnings.
166 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
167 #else
168 # define FOLLY_PUSH_WARNING
169 # define FOLLY_POP_WARNING
170 # define FOLLY_GCC_DISABLE_WARNING(warningName)
171 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
172 #endif
173
174 #ifdef HAVE_SHADOW_LOCAL_WARNINGS
175 #define FOLLY_GCC_DISABLE_NEW_SHADOW_WARNINGS        \
176   FOLLY_GCC_DISABLE_WARNING("-Wshadow-compatible-local") \
177   FOLLY_GCC_DISABLE_WARNING("-Wshadow-local")
178 #else
179 #define FOLLY_GCC_DISABLE_NEW_SHADOW_WARNINGS /* empty */
180 #endif
181
182 #if defined(__GNUC__) && !defined(__APPLE__) && !__GNUC_PREREQ(4,9)
183 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
184 // gcc 4.8.x incorrectly placed max_align_t in the root namespace
185 // Alias it into std (where it's found in 4.9 and later)
186 namespace std { typedef ::max_align_t max_align_t; }
187 #endif
188
189 // portable version check for clang
190 #ifndef __CLANG_PREREQ
191 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
192 /* nolint */
193 #  define __CLANG_PREREQ(maj, min) \
194     ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
195 # else
196 /* nolint */
197 #  define __CLANG_PREREQ(maj, min) 0
198 # endif
199 #endif
200
201 /* Platform specific TLS support
202  * gcc implements __thread
203  * msvc implements __declspec(thread)
204  * the semantics are the same
205  * (but remember __thread has different semantics when using emutls (ex. apple))
206  */
207 #if defined(_MSC_VER)
208 # define FOLLY_TLS __declspec(thread)
209 #elif defined(__GNUC__) || defined(__clang__)
210 # define FOLLY_TLS __thread
211 #else
212 # error cannot define platform specific thread local storage
213 #endif
214
215 #if FOLLY_MOBILE
216 #undef FOLLY_TLS
217 #endif
218
219 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
220 // the 'std' namespace; the latter uses inline namespaces. Wrap this decision
221 // up in a macro to make forward-declarations easier.
222 #if FOLLY_USE_LIBCPP
223 #include <__config>
224 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
225 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
226 #else
227 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
228 #define FOLLY_NAMESPACE_STD_END       }
229 #endif
230
231 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
232 // some types, e.g. std::list.
233 #if _GLIBCXX_USE_CXX11_ABI
234 #define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN \
235   inline _GLIBCXX_BEGIN_NAMESPACE_CXX11
236 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END   _GLIBCXX_END_NAMESPACE_CXX11
237 #else
238 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
239 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
240 #endif
241
242 // MSVC specific defines
243 // mainly for posix compat
244 #ifdef _MSC_VER
245 #include <folly/portability/SysTypes.h>
246
247 // compiler specific to compiler specific
248 // nolint
249 # define __PRETTY_FUNCTION__ __FUNCSIG__
250
251 // Hide a GCC specific thing that breaks MSVC if left alone.
252 # define __extension__
253
254 // We have compiler support for the newest of the new, but
255 // MSVC doesn't tell us that.
256 #define __SSE4_2__ 1
257
258 #endif
259
260 // Debug
261 namespace folly {
262 #ifdef NDEBUG
263 constexpr auto kIsDebug = false;
264 #else
265 constexpr auto kIsDebug = true;
266 #endif
267 }
268
269 // Endianness
270 namespace folly {
271 #ifdef _MSC_VER
272 // It's MSVC, so we just have to guess ... and allow an override
273 #ifdef FOLLY_ENDIAN_BE
274 constexpr auto kIsLittleEndian = false;
275 #else
276 constexpr auto kIsLittleEndian = true;
277 #endif
278 #else
279 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
280 #endif
281 constexpr auto kIsBigEndian = !kIsLittleEndian;
282 }
283
284 #ifndef FOLLY_SSE
285 # if defined(__SSE4_2__)
286 #  define FOLLY_SSE 4
287 #  define FOLLY_SSE_MINOR 2
288 # elif defined(__SSE4_1__)
289 #  define FOLLY_SSE 4
290 #  define FOLLY_SSE_MINOR 1
291 # elif defined(__SSE4__)
292 #  define FOLLY_SSE 4
293 #  define FOLLY_SSE_MINOR 0
294 # elif defined(__SSE3__)
295 #  define FOLLY_SSE 3
296 #  define FOLLY_SSE_MINOR 0
297 # elif defined(__SSE2__)
298 #  define FOLLY_SSE 2
299 #  define FOLLY_SSE_MINOR 0
300 # elif defined(__SSE__)
301 #  define FOLLY_SSE 1
302 #  define FOLLY_SSE_MINOR 0
303 # else
304 #  define FOLLY_SSE 0
305 #  define FOLLY_SSE_MINOR 0
306 # endif
307 #endif
308
309 #define FOLLY_SSE_PREREQ(major, minor) \
310   (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
311
312 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
313 namespace FOLLY_GFLAGS_NAMESPACE { }
314 namespace gflags {
315 using namespace FOLLY_GFLAGS_NAMESPACE;
316 }  // namespace gflags
317 #endif
318
319 // for TARGET_OS_IPHONE
320 #ifdef __APPLE__
321 #include <TargetConditionals.h>
322 #endif
323
324 // RTTI may not be enabled for this compilation unit.
325 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
326     (defined(_MSC_VER) && defined(_CPPRTTI))
327 # define FOLLY_HAS_RTTI 1
328 #endif
329
330 #if defined(__APPLE__) || defined(_MSC_VER)
331 #define FOLLY_STATIC_CTOR_PRIORITY_MAX
332 #else
333 // 101 is the highest priority allowed by the init_priority attribute.
334 // This priority is already used by JEMalloc and other memory allocators so
335 // we will take the next one.
336 #define FOLLY_STATIC_CTOR_PRIORITY_MAX __attribute__((__init_priority__(102)))
337 #endif
338
339 namespace folly {
340
341 #if __OBJC__
342 constexpr auto kIsObjC = true;
343 #else
344 constexpr auto kIsObjC = false;
345 #endif
346
347 #if defined(__linux__) && !FOLLY_MOBILE
348 constexpr auto kIsLinux = true;
349 #else
350 constexpr auto kIsLinux = false;
351 #endif
352
353 #if defined(_WIN32)
354 constexpr auto kIsWindows = true;
355 #else
356 constexpr auto kIsWindows = false;
357 #endif
358 }