More MSan/ASan annotations.
[oota-llvm.git] / include / llvm / Support / Compiler.h
1 //===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines several macros, based on the current compiler.  This allows
11 // use of compiler-specific features in a way that remains portable.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_COMPILER_H
16 #define LLVM_SUPPORT_COMPILER_H
17
18 #include "llvm/Config/llvm-config.h"
19
20 #ifndef __has_feature
21 # define __has_feature(x) 0
22 #endif
23
24 /// \brief Does the compiler support r-value references?
25 /// This implies that <utility> provides the one-argument std::move;  it
26 /// does not imply the existence of any other C++ library features.
27 #if (__has_feature(cxx_rvalue_references)   \
28      || defined(__GXX_EXPERIMENTAL_CXX0X__) \
29      || (defined(_MSC_VER) && _MSC_VER >= 1600))
30 #define LLVM_HAS_RVALUE_REFERENCES 1
31 #else
32 #define LLVM_HAS_RVALUE_REFERENCES 0
33 #endif
34
35 /// \brief Does the compiler support r-value reference *this?
36 ///
37 /// Sadly, this is separate from just r-value reference support because GCC
38 /// implemented everything but this thus far. No release of GCC yet has support
39 /// for this feature so it is enabled with Clang only.
40 /// FIXME: This should change to a version check when GCC grows support for it.
41 #if __has_feature(cxx_rvalue_references)
42 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
43 #else
44 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
45 #endif
46
47 /// \macro LLVM_HAS_CXX11_TYPETRAITS
48 /// \brief Does the compiler have the C++11 type traits.
49 ///
50 /// #include <type_traits>
51 ///
52 /// * enable_if
53 /// * {true,false}_type
54 /// * is_constructible
55 /// * etc...
56 #if defined(__GXX_EXPERIMENTAL_CXX0X__) \
57     || (defined(_MSC_VER) && _MSC_VER >= 1700)
58 #define LLVM_HAS_CXX11_TYPETRAITS 1
59 #else
60 #define LLVM_HAS_CXX11_TYPETRAITS 0
61 #endif
62
63 /// \macro LLVM_HAS_CXX11_STDLIB
64 /// \brief Does the compiler have the C++11 standard library.
65 ///
66 /// Implies LLVM_HAS_RVALUE_REFERENCES, LLVM_HAS_CXX11_TYPETRAITS
67 #if defined(__GXX_EXPERIMENTAL_CXX0X__) \
68     || (defined(_MSC_VER) && _MSC_VER >= 1700)
69 #define LLVM_HAS_CXX11_STDLIB 1
70 #else
71 #define LLVM_HAS_CXX11_STDLIB 0
72 #endif
73
74 /// llvm_move - Expands to ::std::move if the compiler supports
75 /// r-value references; otherwise, expands to the argument.
76 #if LLVM_HAS_RVALUE_REFERENCES
77 #define llvm_move(value) (::std::move(value))
78 #else
79 #define llvm_move(value) (value)
80 #endif
81
82 /// Expands to '&' if r-value references are supported.
83 ///
84 /// This can be used to provide l-value/r-value overrides of member functions.
85 /// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
86 #if LLVM_HAS_RVALUE_REFERENCE_THIS
87 #define LLVM_LVALUE_FUNCTION &
88 #else
89 #define LLVM_LVALUE_FUNCTION
90 #endif
91
92 /// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it.
93 /// Use to mark functions as uncallable. Member functions with this should
94 /// be declared private so that some behavior is kept in C++03 mode.
95 ///
96 /// class DontCopy {
97 /// private:
98 ///   DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
99 ///   DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
100 /// public:
101 ///   ...
102 /// };
103 #if (__has_feature(cxx_deleted_functions) \
104      || defined(__GXX_EXPERIMENTAL_CXX0X__))
105      // No version of MSVC currently supports this.
106 #define LLVM_DELETED_FUNCTION = delete
107 #else
108 #define LLVM_DELETED_FUNCTION
109 #endif
110
111 /// LLVM_FINAL - Expands to 'final' if the compiler supports it.
112 /// Use to mark classes or virtual methods as final.
113 #if __has_feature(cxx_override_control) \
114     || (defined(_MSC_VER) && _MSC_VER >= 1700)
115 #define LLVM_FINAL final
116 #else
117 #define LLVM_FINAL
118 #endif
119
120 /// LLVM_OVERRIDE - Expands to 'override' if the compiler supports it.
121 /// Use to mark virtual methods as overriding a base class method.
122 #if __has_feature(cxx_override_control) \
123     || (defined(_MSC_VER) && _MSC_VER >= 1700)
124 #define LLVM_OVERRIDE override
125 #else
126 #define LLVM_OVERRIDE
127 #endif
128
129 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
130 # define LLVM_CONSTEXPR constexpr
131 #else
132 # define LLVM_CONSTEXPR
133 #endif
134
135 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
136 /// into a shared library, then the class should be private to the library and
137 /// not accessible from outside it.  Can also be used to mark variables and
138 /// functions, making them private to any shared library they are linked into.
139 #if (__GNUC__ >= 4) && !defined(__MINGW32__) && !defined(__CYGWIN__)
140 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
141 #else
142 #define LLVM_LIBRARY_VISIBILITY
143 #endif
144
145 #if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
146 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
147 #else
148 #define LLVM_ATTRIBUTE_USED
149 #endif
150
151 // Some compilers warn about unused functions. When a function is sometimes
152 // used or not depending on build settings (e.g. a function only called from
153 // within "assert"), this attribute can be used to suppress such warnings.
154 //
155 // However, it shouldn't be used for unused *variables*, as those have a much
156 // more portable solution:
157 //   (void)unused_var_name;
158 // Prefer cast-to-void wherever it is sufficient.
159 #if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
160 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
161 #else
162 #define LLVM_ATTRIBUTE_UNUSED
163 #endif
164
165 #if (__GNUC__ >= 4) && !defined(__MINGW32__) && !defined(__CYGWIN__)
166 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
167 #else
168 #define LLVM_ATTRIBUTE_WEAK
169 #endif
170
171 #ifdef __GNUC__ // aka 'CONST' but following LLVM Conventions.
172 #define LLVM_READNONE __attribute__((__const__))
173 #else
174 #define LLVM_READNONE
175 #endif
176
177 #ifdef __GNUC__  // aka 'PURE' but following LLVM Conventions.
178 #define LLVM_READONLY __attribute__((__pure__))
179 #else
180 #define LLVM_READONLY
181 #endif
182
183 #if (__GNUC__ >= 4)
184 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
185 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
186 #else
187 #define LLVM_LIKELY(EXPR) (EXPR)
188 #define LLVM_UNLIKELY(EXPR) (EXPR)
189 #endif
190
191 // C++ doesn't support 'extern template' of template specializations.  GCC does,
192 // but requires __extension__ before it.  In the header, use this:
193 //   EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
194 // in the .cpp file, use this:
195 //   TEMPLATE_INSTANTIATION(class foo<bar>);
196 #ifdef __GNUC__
197 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
198 #define TEMPLATE_INSTANTIATION(X) template X
199 #else
200 #define EXTERN_TEMPLATE_INSTANTIATION(X)
201 #define TEMPLATE_INSTANTIATION(X)
202 #endif
203
204 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
205 /// mark a method "not for inlining".
206 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
207 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
208 #elif defined(_MSC_VER)
209 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
210 #else
211 #define LLVM_ATTRIBUTE_NOINLINE
212 #endif
213
214 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
215 /// so, mark a method "always inline" because it is performance sensitive. GCC
216 /// 3.4 supported this but is buggy in various cases and produces unimplemented
217 /// errors, just use it in GCC 4.0 and later.
218 #if __GNUC__ > 3
219 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
220 #elif defined(_MSC_VER)
221 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
222 #else
223 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
224 #endif
225
226 #ifdef __GNUC__
227 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
228 #elif defined(_MSC_VER)
229 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
230 #else
231 #define LLVM_ATTRIBUTE_NORETURN
232 #endif
233
234 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
235 /// pedantic diagnostics.
236 #ifdef __GNUC__
237 #define LLVM_EXTENSION __extension__
238 #else
239 #define LLVM_EXTENSION
240 #endif
241
242 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
243 #if __has_feature(attribute_deprecated_with_message)
244 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
245   decl __attribute__((deprecated(message)))
246 #elif defined(__GNUC__)
247 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
248   decl __attribute__((deprecated))
249 #elif defined(_MSC_VER)
250 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
251   __declspec(deprecated(message)) decl
252 #else
253 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
254   decl
255 #endif
256
257 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
258 /// to an expression which states that it is undefined behavior for the
259 /// compiler to reach this point.  Otherwise is not defined.
260 #if defined(__clang__) || (__GNUC__ > 4) \
261  || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
262 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
263 #elif defined(_MSC_VER)
264 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
265 #endif
266
267 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
268 /// which causes the program to exit abnormally.
269 #if defined(__clang__) || (__GNUC__ > 4) \
270  || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
271 # define LLVM_BUILTIN_TRAP __builtin_trap()
272 #else
273 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
274 #endif
275
276 /// \macro LLVM_ASSUME_ALIGNED
277 /// \brief Returns a pointer with an assumed alignment.
278 #if !defined(__clang__) && ((__GNUC__ > 4) \
279  || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
280 // FIXME: Enable on clang when it supports it.
281 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
282 #elif defined(LLVM_BUILTIN_UNREACHABLE)
283 # define LLVM_ASSUME_ALIGNED(p, a) \
284            (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
285 #else
286 # define LLVM_ASSUME_ALIGNED(p, a) (p)
287 #endif
288
289 /// \macro LLVM_FUNCTION_NAME
290 /// \brief Expands to __func__ on compilers which support it.  Otherwise,
291 /// expands to a compiler-dependent replacement.
292 #if defined(_MSC_VER)
293 # define LLVM_FUNCTION_NAME __FUNCTION__
294 #else
295 # define LLVM_FUNCTION_NAME __func__
296 #endif
297
298 #if defined(LLVM_HAVE_MSAN_ANNOTATIONS)
299 # include <sanitizer/msan_interface.h>
300 #else
301 # define __msan_allocated_memory(p, size)
302 # define __msan_unpoison(p, size)
303 #endif
304
305 /// \macro LLVM_MEMORY_SANITIZER_BUILD
306 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
307 #if __has_feature(memory_sanitizer)
308 # define LLVM_MEMORY_SANITIZER_BUILD 1
309 #else
310 # define LLVM_MEMORY_SANITIZER_BUILD 0
311 #endif
312
313 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
314 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
315 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
316 # define LLVM_ADDRESS_SANITIZER_BUILD 1
317 #else
318 # define LLVM_ADDRESS_SANITIZER_BUILD 0
319 #endif
320
321 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
322 /// \brief Is unaligned memory access fast on the host machine.
323 ///
324 /// Don't specialize on alignment for platforms where unaligned memory accesses
325 /// generates the same code as aligned memory accesses for common types.
326 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
327     defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
328     defined(_X86_) || defined(__i386) || defined(__i386__)
329 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
330 #else
331 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
332 #endif
333
334 #endif