printf format checking for msvc
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2014 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 #ifndef FOLLY_PORTABILITY_H_
18 #define FOLLY_PORTABILITY_H_
19
20 #ifndef FOLLY_NO_CONFIG
21 #include "folly-config.h"
22 #endif
23
24 #if FOLLY_HAVE_FEATURES_H
25 #include <features.h>
26 #endif
27
28 #include "CPortability.h"
29
30 #if FOLLY_HAVE_SCHED_H
31  #include <sched.h>
32  #ifndef FOLLY_HAVE_PTHREAD_YIELD
33   #define pthread_yield sched_yield
34  #endif
35 #endif
36
37
38 // MaxAlign: max_align_t isn't supported by gcc
39 #ifdef __GNUC__
40 struct MaxAlign { char c; } __attribute__((aligned));
41 #else /* !__GNUC__ */
42 # error Cannot define MaxAlign on this platform
43 #endif
44
45 // compiler specific attribute translation
46 // msvc should come first, so if clang is in msvc mode it gets the right defines
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 # define _USE_ATTRIBUTES_FOR_SAL 1
54 # include <sal.h>
55 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
56 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
57 #else
58 # define FOLLY_PRINTF_FORMAT /**/
59 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
60   __attribute__((format(printf, format_param, dots_param)))
61 #endif
62
63 // noreturn
64 #if defined(_MSC_VER)
65 # define FOLLY_NORETURN __declspec(noreturn)
66 #elif defined(__clang__) || defined(__GNUC__)
67 # define FOLLY_NORETURN __attribute__((noreturn))
68 #else
69 # define FOLLY_NORETURN
70 #endif
71
72 // noinline
73 #ifdef _MSC_VER
74 # define FOLLY_NOINLINE __declspec(noinline)
75 #elif defined(__clang__) || defined(__GNUC__)
76 # define FOLLY_NOINLINE __attribute__((noinline))
77 #else
78 # define FOLLY_NOINLINE
79 #endif
80
81 // always inline
82 #ifdef _MSC_VER
83 # define FOLLY_ALWAYS_INLINE __forceinline
84 #elif defined(__clang__) || defined(__GNUC__)
85 # define FOLLY_ALWAYS_INLINE inline __attribute__((always_inline))
86 #else
87 # define FOLLY_ALWAYS_INLINE
88 #endif
89
90 // detection for 64 bit
91 #if defined(__x86_64__) || defined(_M_X64)
92 # define FOLLY_X64 1
93 #else
94 # define FOLLY_X64 0
95 #endif
96
97 // packing is very ugly in msvc
98 #ifdef _MSC_VER
99 # define FOLLY_PACK_ATTR /**/
100 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
101 # define FOLLY_PACK_POP __pragma(pack(pop))
102 #elif defined(__clang__) || defined(__GNUC__)
103 # define FOLLY_PACK_ATTR __attribute__((packed))
104 # define FOLLY_PACK_PUSH /**/
105 # define FOLLY_PACK_POP /**/
106 #else
107 # define FOLLY_PACK_ATTR /**/
108 # define FOLLY_PACK_PUSH /**/
109 # define FOLLY_PACK_POP /**/
110 #endif
111
112 // portable version check
113 #ifndef __GNUC_PREREQ
114 # if defined __GNUC__ && defined __GNUC_MINOR__
115 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
116                                    ((maj) << 16) + (min))
117 # else
118 #  define __GNUC_PREREQ(maj, min) 0
119 # endif
120 #endif
121
122
123 /* Define macro wrappers for C++11's "final" and "override" keywords, which
124  * are supported in gcc 4.7 but not gcc 4.6. */
125 #if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
126 # if defined(__clang__) || __GNUC_PREREQ(4, 7)
127 #  define FOLLY_FINAL final
128 #  define FOLLY_OVERRIDE override
129 # else
130 #  define FOLLY_FINAL /**/
131 #  define FOLLY_OVERRIDE /**/
132 # endif
133 #endif
134
135 /* Platform specific TLS support
136  * gcc implements __thread
137  * msvc implements __declspec(thread)
138  * the semantics are the same (but remember __thread is broken on apple)
139  */
140 #if defined(_MSC_VER)
141 # define FOLLY_TLS __declspec(thread)
142 #elif defined(__GNUC__) || defined(__clang__)
143 # define FOLLY_TLS __thread
144 #else
145 # error cannot define platform specific thread local storage
146 #endif
147
148 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
149 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
150 # if defined(__GLIBC_PREREQ)
151 #  if __GLIBC_PREREQ(2, 10)
152 #   define FOLLY_HAVE_PREADV 1
153 #   define FOLLY_HAVE_PWRITEV 1
154 #  endif
155 # endif
156 #endif
157
158 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
159 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
160 // up in a macro to make forward-declarations easier.
161 #if FOLLY_USE_LIBCPP
162 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
163 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
164 #else
165 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
166 #define FOLLY_NAMESPACE_STD_END       }
167 #endif
168
169 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
170 // versions of these into the global namespace.
171 #if FOLLY_HAVE_CLOCK_GETTIME
172 #include <time.h>
173 #else
174 #include "folly/detail/Clock.h"
175 #endif
176
177 // Provide our own std::__throw_* wrappers for platforms that don't have them
178 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
179 #include <bits/functexcept.h>
180 #else
181 #include "folly/detail/FunctionalExcept.h"
182 #endif
183
184 #if defined(__cplusplus)
185 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
186 // usage of __has_trivial_copy(), so we can't use it as a
187 // least-common-denominator for C++11 implementations that don't support
188 // std::is_trivially_copyable<T>.
189 //
190 //      http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
191 //
192 // As a result, use std::is_trivially_copyable() where it exists, and fall back
193 // to Boost otherwise.
194 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
195 #include <type_traits>
196 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
197   (std::is_trivially_copyable<T>::value)
198 #else
199 #include <boost/type_traits.hpp>
200 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
201   (boost::has_trivial_copy<T>::value &&                  \
202    boost::has_trivial_destructor<T>::value)
203 #endif
204 #endif // __cplusplus
205
206 // MSVC specific defines
207 // mainly for posix compat
208 #ifdef _MSC_VER
209
210 // this definition is in a really silly place with a silly name
211 // and ifdefing it every time we want it is painful
212 #include <basetsd.h>
213 typedef SSIZE_T ssize_t;
214
215 // sprintf semantics are not exactly identical
216 // but current usage is not a problem
217 # define snprintf _snprintf
218
219 // semantics here are identical
220 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
221
222 // compiler specific to compiler specific
223 # define __PRETTY_FUNCTION__ __FUNCSIG__
224 #endif
225
226 #endif // FOLLY_PORTABILITY_H_