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