Revert D6745720: [folly][compression] Log (de)compression bytes
[folly.git] / folly / CPortability.h
1 /*
2  * Copyright 2013-present 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 /* These definitions are in a separate file so that they
20  * may be included from C- as well as C++-based projects. */
21
22 /**
23  * Portable version check.
24  */
25 #ifndef __GNUC_PREREQ
26 # if defined __GNUC__ && defined __GNUC_MINOR__
27 /* nolint */
28 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
29                                    ((maj) << 16) + (min))
30 # else
31 /* nolint */
32 #  define __GNUC_PREREQ(maj, min) 0
33 # endif
34 #endif
35
36 // portable version check for clang
37 #ifndef __CLANG_PREREQ
38 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
39 /* nolint */
40 #  define __CLANG_PREREQ(maj, min) \
41     ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
42 # else
43 /* nolint */
44 #  define __CLANG_PREREQ(maj, min) 0
45 # endif
46 #endif
47
48 #if defined(__has_builtin)
49 #define FOLLY_HAS_BUILTIN(...) __has_builtin(__VA_ARGS__)
50 #else
51 #define FOLLY_HAS_BUILTIN(...) 0
52 #endif
53
54 #if defined(__has_feature)
55 #define FOLLY_HAS_FEATURE(...) __has_feature(__VA_ARGS__)
56 #else
57 #define FOLLY_HAS_FEATURE(...) 0
58 #endif
59
60 #if defined(__has_include)
61 #define FOLLY_HAS_INCLUDE(...) __has_include(__VA_ARGS__)
62 #else
63 #define FOLLY_HAS_INCLUDE(...) 0
64 #endif
65
66 /* Define a convenience macro to test when address sanitizer is being used
67  * across the different compilers (e.g. clang, gcc) */
68 #if FOLLY_HAS_FEATURE(address_sanitizer) || __SANITIZE_ADDRESS__
69 # define FOLLY_SANITIZE_ADDRESS 1
70 #endif
71
72 /* Define attribute wrapper for function attribute used to disable
73  * address sanitizer instrumentation. Unfortunately, this attribute
74  * has issues when inlining is used, so disable that as well. */
75 #ifdef FOLLY_SANITIZE_ADDRESS
76 # if defined(__clang__)
77 #  if __has_attribute(__no_sanitize__)
78 #   define FOLLY_DISABLE_ADDRESS_SANITIZER \
79       __attribute__((__no_sanitize__("address"), __noinline__))
80 #  elif __has_attribute(__no_address_safety_analysis__)
81 #   define FOLLY_DISABLE_ADDRESS_SANITIZER \
82       __attribute__((__no_address_safety_analysis__, __noinline__))
83 #  elif __has_attribute(__no_sanitize_address__)
84 #   define FOLLY_DISABLE_ADDRESS_SANITIZER \
85       __attribute__((__no_sanitize_address__, __noinline__))
86 #  endif
87 # elif defined(__GNUC__)
88 #  define FOLLY_DISABLE_ADDRESS_SANITIZER \
89      __attribute__((__no_address_safety_analysis__, __noinline__))
90 # endif
91 #endif
92 #ifndef FOLLY_DISABLE_ADDRESS_SANITIZER
93 # define FOLLY_DISABLE_ADDRESS_SANITIZER
94 #endif
95
96 /* Define a convenience macro to test when thread sanitizer is being used
97  * across the different compilers (e.g. clang, gcc) */
98 #if FOLLY_HAS_FEATURE(thread_sanitizer) || __SANITIZE_THREAD__
99 # define FOLLY_SANITIZE_THREAD 1
100 #endif
101
102 /**
103  * Define a convenience macro to test when ASAN, UBSAN or TSAN sanitizer are
104  * being used
105  */
106 #if defined(FOLLY_SANITIZE_ADDRESS) || defined(FOLLY_SANITIZE_THREAD) || \
107     defined(UNDEFINED_SANITIZER)
108 #define FOLLY_SANITIZE 1
109 #endif
110
111 /**
112  * ASAN/MSAN/TSAN define pre-processor symbols:
113  * ADDRESS_SANITIZER/MEMORY_SANITIZER/THREAD_SANITIZER.
114  *
115  * UBSAN doesn't define anything and makes it hard to
116  * conditionally compile.
117  *
118  * The build system should define UNDEFINED_SANITIZER=1 when UBSAN is
119  * used as folly whitelists some functions.
120  */
121 #if UNDEFINED_SANITIZER
122 #define FOLLY_DISABLE_UNDEFINED_BEHAVIOR_SANITIZER(...) \
123   __attribute__((no_sanitize(__VA_ARGS__)))
124 #else
125 #define FOLLY_DISABLE_UNDEFINED_BEHAVIOR_SANITIZER(...)
126 #endif // UNDEFINED_SANITIZER
127
128 /**
129  * Macro for marking functions as having public visibility.
130  */
131 #if defined(__GNUC__)
132 # if __GNUC_PREREQ(4, 9)
133 #  define FOLLY_EXPORT [[gnu::visibility("default")]]
134 # else
135 #  define FOLLY_EXPORT __attribute__((__visibility__("default")))
136 # endif
137 #else
138 # define FOLLY_EXPORT
139 #endif
140
141 // noinline
142 #ifdef _MSC_VER
143 # define FOLLY_NOINLINE __declspec(noinline)
144 #elif defined(__clang__) || defined(__GNUC__)
145 # define FOLLY_NOINLINE __attribute__((__noinline__))
146 #else
147 # define FOLLY_NOINLINE
148 #endif
149
150 // always inline
151 #ifdef _MSC_VER
152 # define FOLLY_ALWAYS_INLINE __forceinline
153 #elif defined(__clang__) || defined(__GNUC__)
154 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
155 #else
156 # define FOLLY_ALWAYS_INLINE inline
157 #endif
158
159 // attribute hidden
160 #if _MSC_VER
161 #define FOLLY_ATTR_VISIBILITY_HIDDEN
162 #elif defined(__clang__) || defined(__GNUC__)
163 #define FOLLY_ATTR_VISIBILITY_HIDDEN __attribute__((__visibility__("hidden")))
164 #else
165 #define FOLLY_ATTR_VISIBILITY_HIDDEN
166 #endif