Add FOLLY_HAS_BUILTIN and use it
[folly.git] / folly / CPortability.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 /* 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 /* Define a convenience macro to test when address sanitizer is being used
55  * across the different compilers (e.g. clang, gcc) */
56 #if defined(__clang__)
57 # if __has_feature(address_sanitizer)
58 #  define FOLLY_SANITIZE_ADDRESS 1
59 # endif
60 #elif defined (__GNUC__) && \
61       (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || (__GNUC__ >= 5)) && \
62       __SANITIZE_ADDRESS__
63 # define FOLLY_SANITIZE_ADDRESS 1
64 #endif
65
66 /* Define attribute wrapper for function attribute used to disable
67  * address sanitizer instrumentation. Unfortunately, this attribute
68  * has issues when inlining is used, so disable that as well. */
69 #ifdef FOLLY_SANITIZE_ADDRESS
70 # if defined(__clang__)
71 #  if __has_attribute(__no_sanitize__)
72 #   define FOLLY_DISABLE_ADDRESS_SANITIZER \
73       __attribute__((__no_sanitize__("address"), __noinline__))
74 #  elif __has_attribute(__no_address_safety_analysis__)
75 #   define FOLLY_DISABLE_ADDRESS_SANITIZER \
76       __attribute__((__no_address_safety_analysis__, __noinline__))
77 #  elif __has_attribute(__no_sanitize_address__)
78 #   define FOLLY_DISABLE_ADDRESS_SANITIZER \
79       __attribute__((__no_sanitize_address__, __noinline__))
80 #  endif
81 # elif defined(__GNUC__)
82 #  define FOLLY_DISABLE_ADDRESS_SANITIZER \
83      __attribute__((__no_address_safety_analysis__, __noinline__))
84 # endif
85 #endif
86 #ifndef FOLLY_DISABLE_ADDRESS_SANITIZER
87 # define FOLLY_DISABLE_ADDRESS_SANITIZER
88 #endif
89
90 /* Define a convenience macro to test when thread sanitizer is being used
91  * across the different compilers (e.g. clang, gcc) */
92 #if defined(__clang__)
93 # if __has_feature(thread_sanitizer)
94 #  define FOLLY_SANITIZE_THREAD 1
95 # endif
96 #elif defined(__GNUC__) && __SANITIZE_THREAD__
97 # define FOLLY_SANITIZE_THREAD 1
98 #endif
99
100 /**
101  * ASAN/MSAN/TSAN define pre-processor symbols:
102  * ADDRESS_SANITIZER/MEMORY_SANITIZER/THREAD_SANITIZER.
103  *
104  * UBSAN doesn't define anything and makes it hard to
105  * conditionally compile.
106  *
107  * The build system should define UNDEFINED_SANITIZER=1 when UBSAN is
108  * used as folly whitelists some functions.
109  */
110 #if UNDEFINED_SANITIZER
111 #define FOLLY_DISABLE_UNDEFINED_BEHAVIOR_SANITIZER(...) \
112   __attribute__((no_sanitize(__VA_ARGS__)))
113 #else
114 #define FOLLY_DISABLE_UNDEFINED_BEHAVIOR_SANITIZER(...)
115 #endif // UNDEFINED_SANITIZER
116
117 /**
118  * Macro for marking functions as having public visibility.
119  */
120 #if defined(__GNUC__)
121 # if __GNUC_PREREQ(4, 9)
122 #  define FOLLY_EXPORT [[gnu::visibility("default")]]
123 # else
124 #  define FOLLY_EXPORT __attribute__((__visibility__("default")))
125 # endif
126 #else
127 # define FOLLY_EXPORT
128 #endif
129
130 // noinline
131 #ifdef _MSC_VER
132 # define FOLLY_NOINLINE __declspec(noinline)
133 #elif defined(__clang__) || defined(__GNUC__)
134 # define FOLLY_NOINLINE __attribute__((__noinline__))
135 #else
136 # define FOLLY_NOINLINE
137 #endif
138
139 // always inline
140 #ifdef _MSC_VER
141 # define FOLLY_ALWAYS_INLINE __forceinline
142 #elif defined(__clang__) || defined(__GNUC__)
143 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
144 #else
145 # define FOLLY_ALWAYS_INLINE inline
146 #endif
147
148 // attribute hidden
149 #if _MSC_VER
150 #define FOLLY_ATTR_VISIBILITY_HIDDEN
151 #elif defined(__clang__) || defined(__GNUC__)
152 #define FOLLY_ATTR_VISIBILITY_HIDDEN __attribute__((__visibility__("hidden")))
153 #else
154 #define FOLLY_ATTR_VISIBILITY_HIDDEN
155 #endif