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 #if defined(_MSC_VER)
21 #include <sal.h>
22 #endif
23 
24 #ifndef __has_feature
25 # define __has_feature(x) 0
26 #endif
27 
28 #ifndef __has_extension
29 # define __has_extension(x) 0
30 #endif
31 
32 #ifndef __has_attribute
33 # define __has_attribute(x) 0
34 #endif
35 
36 #ifndef __has_builtin
37 # define __has_builtin(x) 0
38 #endif
39 
40 /// \macro LLVM_GNUC_PREREQ
41 /// \brief Extend the default __GNUC_PREREQ even if glibc's features.h isn't
42 /// available.
43 #ifndef LLVM_GNUC_PREREQ
44 # if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
45 #  define LLVM_GNUC_PREREQ(maj, min, patch) \
46     ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
47      ((maj) << 20) + ((min) << 10) + (patch))
48 # elif defined(__GNUC__) && defined(__GNUC_MINOR__)
49 #  define LLVM_GNUC_PREREQ(maj, min, patch) \
50     ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
51 # else
52 #  define LLVM_GNUC_PREREQ(maj, min, patch) 0
53 # endif
54 #endif
55 
56 /// \macro LLVM_MSC_PREREQ
57 /// \brief Is the compiler MSVC of at least the specified version?
58 /// The common \param version values to check for are:
59 ///  * 1800: Microsoft Visual Studio 2013 / 12.0
60 ///  * 1900: Microsoft Visual Studio 2015 / 14.0
61 #ifdef _MSC_VER
62 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
63 
64 // We require at least MSVC 2013.
65 #if !LLVM_MSC_PREREQ(1800)
66 #error LLVM requires at least MSVC 2013.
67 #endif
68 
69 #else
70 #define LLVM_MSC_PREREQ(version) 0
71 #endif
72 
73 #if !defined(_MSC_VER) || defined(__clang__) || LLVM_MSC_PREREQ(1900)
74 #define LLVM_NOEXCEPT noexcept
75 #else
76 #define LLVM_NOEXCEPT throw()
77 #endif
78 
79 /// \brief Does the compiler support ref-qualifiers for *this?
80 ///
81 /// Sadly, this is separate from just rvalue reference support because GCC
82 /// and MSVC implemented this later than everything else.
83 #if __has_feature(cxx_rvalue_references) || LLVM_GNUC_PREREQ(4, 8, 1)
84 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
85 #else
86 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
87 #endif
88 
89 /// Expands to '&' if ref-qualifiers for *this are supported.
90 ///
91 /// This can be used to provide lvalue/rvalue overrides of member functions.
92 /// The rvalue override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
93 #if LLVM_HAS_RVALUE_REFERENCE_THIS
94 #define LLVM_LVALUE_FUNCTION &
95 #else
96 #define LLVM_LVALUE_FUNCTION
97 #endif
98 
99 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1900)
100 # define LLVM_CONSTEXPR constexpr
101 #else
102 # define LLVM_CONSTEXPR
103 #endif
104 
105 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
106 /// into a shared library, then the class should be private to the library and
107 /// not accessible from outside it.  Can also be used to mark variables and
108 /// functions, making them private to any shared library they are linked into.
109 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
110 #if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) &&              \
111     !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
112 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
113 #else
114 #define LLVM_LIBRARY_VISIBILITY
115 #endif
116 
117 #if __has_attribute(sentinel) || LLVM_GNUC_PREREQ(3, 0, 0)
118 #define LLVM_END_WITH_NULL __attribute__((sentinel))
119 #else
120 #define LLVM_END_WITH_NULL
121 #endif
122 
123 #if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
124 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
125 #else
126 #define LLVM_ATTRIBUTE_USED
127 #endif
128 
129 #if __has_attribute(warn_unused_result) || LLVM_GNUC_PREREQ(3, 4, 0)
130 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
131 #elif defined(_MSC_VER)
132 #define LLVM_ATTRIBUTE_UNUSED_RESULT _Check_return_
133 #else
134 #define LLVM_ATTRIBUTE_UNUSED_RESULT
135 #endif
136 
137 // Some compilers warn about unused functions. When a function is sometimes
138 // used or not depending on build settings (e.g. a function only called from
139 // within "assert"), this attribute can be used to suppress such warnings.
140 //
141 // However, it shouldn't be used for unused *variables*, as those have a much
142 // more portable solution:
143 //   (void)unused_var_name;
144 // Prefer cast-to-void wherever it is sufficient.
145 #if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0)
146 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
147 #else
148 #define LLVM_ATTRIBUTE_UNUSED
149 #endif
150 
151 // FIXME: Provide this for PE/COFF targets.
152 #if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) &&                    \
153     (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
154 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
155 #else
156 #define LLVM_ATTRIBUTE_WEAK
157 #endif
158 
159 // Prior to clang 3.2, clang did not accept any spelling of
160 // __has_attribute(const), so assume it is supported.
161 #if defined(__clang__) || defined(__GNUC__)
162 // aka 'CONST' but following LLVM Conventions.
163 #define LLVM_READNONE __attribute__((__const__))
164 #else
165 #define LLVM_READNONE
166 #endif
167 
168 #if __has_attribute(pure) || defined(__GNUC__)
169 // aka 'PURE' but following LLVM Conventions.
170 #define LLVM_READONLY __attribute__((__pure__))
171 #else
172 #define LLVM_READONLY
173 #endif
174 
175 #if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0)
176 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
177 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
178 #else
179 #define LLVM_LIKELY(EXPR) (EXPR)
180 #define LLVM_UNLIKELY(EXPR) (EXPR)
181 #endif
182 
183 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
184 /// mark a method "not for inlining".
185 #if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
186 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
187 #elif defined(_MSC_VER)
188 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
189 #else
190 #define LLVM_ATTRIBUTE_NOINLINE
191 #endif
192 
193 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
194 /// so, mark a method "always inline" because it is performance sensitive. GCC
195 /// 3.4 supported this but is buggy in various cases and produces unimplemented
196 /// errors, just use it in GCC 4.0 and later.
197 #if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
198 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
199 #elif defined(_MSC_VER)
200 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
201 #else
202 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
203 #endif
204 
205 #ifdef __GNUC__
206 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
207 #elif defined(_MSC_VER)
208 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
209 #else
210 #define LLVM_ATTRIBUTE_NORETURN
211 #endif
212 
213 #if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
214 #define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
215 #elif defined(_MSC_VER)
216 #define LLVM_ATTRIBUTE_RETURNS_NONNULL _Ret_notnull_
217 #else
218 #define LLVM_ATTRIBUTE_RETURNS_NONNULL
219 #endif
220 
221 /// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a
222 /// pointer that does not alias any other valid pointer.
223 #ifdef __GNUC__
224 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
225 #elif defined(_MSC_VER)
226 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
227 #else
228 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS
229 #endif
230 
231 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
232 /// pedantic diagnostics.
233 #ifdef __GNUC__
234 #define LLVM_EXTENSION __extension__
235 #else
236 #define LLVM_EXTENSION
237 #endif
238 
239 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
240 #if __has_feature(attribute_deprecated_with_message)
241 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
242   decl __attribute__((deprecated(message)))
243 #elif defined(__GNUC__)
244 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
245   decl __attribute__((deprecated))
246 #elif defined(_MSC_VER)
247 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
248   __declspec(deprecated(message)) decl
249 #else
250 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
251   decl
252 #endif
253 
254 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
255 /// to an expression which states that it is undefined behavior for the
256 /// compiler to reach this point.  Otherwise is not defined.
257 #if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
258 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
259 #elif defined(_MSC_VER)
260 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
261 #endif
262 
263 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
264 /// which causes the program to exit abnormally.
265 #if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
266 # define LLVM_BUILTIN_TRAP __builtin_trap()
267 #elif defined(_MSC_VER)
268 // The __debugbreak intrinsic is supported by MSVC, does not require forward
269 // declarations involving platform-specific typedefs (unlike RaiseException),
270 // results in a call to vectored exception handlers, and encodes to a short
271 // instruction that still causes the trapping behavior we want.
272 # define LLVM_BUILTIN_TRAP __debugbreak()
273 #else
274 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
275 #endif
276 
277 /// LLVM_BUILTIN_DEBUGTRAP - On compilers which support it, expands to
278 /// an expression which causes the program to break while running
279 /// under a debugger.
280 #if __has_builtin(__builtin_debugtrap)
281 # define LLVM_BUILTIN_DEBUGTRAP __builtin_debugtrap()
282 #elif defined(_MSC_VER)
283 // The __debugbreak intrinsic is supported by MSVC and breaks while
284 // running under the debugger, and also supports invoking a debugger
285 // when the OS is configured appropriately.
286 # define LLVM_BUILTIN_DEBUGTRAP __debugbreak()
287 #else
288 // Just continue execution when built with compilers that have no
289 // support. This is a debugging aid and not intended to force the
290 // program to abort if encountered.
291 # define LLVM_BUILTIN_DEBUGTRAP
292 #endif
293 
294 /// \macro LLVM_ASSUME_ALIGNED
295 /// \brief Returns a pointer with an assumed alignment.
296 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
297 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
298 #elif defined(LLVM_BUILTIN_UNREACHABLE)
299 // As of today, clang does not support __builtin_assume_aligned.
300 # define LLVM_ASSUME_ALIGNED(p, a) \
301            (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
302 #else
303 # define LLVM_ASSUME_ALIGNED(p, a) (p)
304 #endif
305 
306 /// \macro LLVM_ALIGNAS
307 /// \brief Used to specify a minimum alignment for a structure or variable. The
308 /// alignment must be a constant integer. Use LLVM_PTR_SIZE to compute
309 /// alignments in terms of the size of a pointer.
310 ///
311 /// Note that __declspec(align) has special quirks, it's not legal to pass a
312 /// structure with __declspec(align) as a formal parameter.
313 #ifdef _MSC_VER
314 # define LLVM_ALIGNAS(x) __declspec(align(x))
315 #elif __GNUC__ && !__has_feature(cxx_alignas) && !LLVM_GNUC_PREREQ(4, 8, 0)
316 # define LLVM_ALIGNAS(x) __attribute__((aligned(x)))
317 #else
318 # define LLVM_ALIGNAS(x) alignas(x)
319 #endif
320 
321 /// \macro LLVM_PACKED
322 /// \brief Used to specify a packed structure.
323 /// LLVM_PACKED(
324 ///    struct A {
325 ///      int i;
326 ///      int j;
327 ///      int k;
328 ///      long long l;
329 ///   });
330 ///
331 /// LLVM_PACKED_START
332 /// struct B {
333 ///   int i;
334 ///   int j;
335 ///   int k;
336 ///   long long l;
337 /// };
338 /// LLVM_PACKED_END
339 #ifdef _MSC_VER
340 # define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop))
341 # define LLVM_PACKED_START __pragma(pack(push, 1))
342 # define LLVM_PACKED_END   __pragma(pack(pop))
343 #else
344 # define LLVM_PACKED(d) d __attribute__((packed))
345 # define LLVM_PACKED_START _Pragma("pack(push, 1)")
346 # define LLVM_PACKED_END   _Pragma("pack(pop)")
347 #endif
348 
349 /// \macro LLVM_PTR_SIZE
350 /// \brief A constant integer equivalent to the value of sizeof(void*).
351 /// Generally used in combination with LLVM_ALIGNAS or when doing computation in
352 /// the preprocessor.
353 #ifdef __SIZEOF_POINTER__
354 # define LLVM_PTR_SIZE __SIZEOF_POINTER__
355 #elif defined(_WIN64)
356 # define LLVM_PTR_SIZE 8
357 #elif defined(_WIN32)
358 # define LLVM_PTR_SIZE 4
359 #elif defined(_MSC_VER)
360 # error "could not determine LLVM_PTR_SIZE as a constant int for MSVC"
361 #else
362 # define LLVM_PTR_SIZE sizeof(void *)
363 #endif
364 
365 /// \macro LLVM_FUNCTION_NAME
366 /// \brief Expands to __func__ on compilers which support it.  Otherwise,
367 /// expands to a compiler-dependent replacement.
368 #if defined(_MSC_VER)
369 # define LLVM_FUNCTION_NAME __FUNCTION__
370 #else
371 # define LLVM_FUNCTION_NAME __func__
372 #endif
373 
374 /// \macro LLVM_MEMORY_SANITIZER_BUILD
375 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
376 #if __has_feature(memory_sanitizer)
377 # define LLVM_MEMORY_SANITIZER_BUILD 1
378 # include <sanitizer/msan_interface.h>
379 #else
380 # define LLVM_MEMORY_SANITIZER_BUILD 0
381 # define __msan_allocated_memory(p, size)
382 # define __msan_unpoison(p, size)
383 #endif
384 
385 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
386 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
387 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
388 # define LLVM_ADDRESS_SANITIZER_BUILD 1
389 # include <sanitizer/asan_interface.h>
390 #else
391 # define LLVM_ADDRESS_SANITIZER_BUILD 0
392 # define __asan_poison_memory_region(p, size)
393 # define __asan_unpoison_memory_region(p, size)
394 #endif
395 
396 /// \macro LLVM_THREAD_SANITIZER_BUILD
397 /// \brief Whether LLVM itself is built with ThreadSanitizer instrumentation.
398 #if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
399 # define LLVM_THREAD_SANITIZER_BUILD 1
400 #else
401 # define LLVM_THREAD_SANITIZER_BUILD 0
402 #endif
403 
404 #if LLVM_THREAD_SANITIZER_BUILD
405 // Thread Sanitizer is a tool that finds races in code.
406 // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
407 // tsan detects these exact functions by name.
408 extern "C" {
409 void AnnotateHappensAfter(const char *file, int line, const volatile void *cv);
410 void AnnotateHappensBefore(const char *file, int line, const volatile void *cv);
411 void AnnotateIgnoreWritesBegin(const char *file, int line);
412 void AnnotateIgnoreWritesEnd(const char *file, int line);
413 }
414 
415 // This marker is used to define a happens-before arc. The race detector will
416 // infer an arc from the begin to the end when they share the same pointer
417 // argument.
418 # define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv)
419 
420 // This marker defines the destination of a happens-before arc.
421 # define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv)
422 
423 // Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
424 # define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
425 
426 // Resume checking for racy writes.
427 # define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
428 #else
429 # define TsanHappensBefore(cv)
430 # define TsanHappensAfter(cv)
431 # define TsanIgnoreWritesBegin()
432 # define TsanIgnoreWritesEnd()
433 #endif
434 
435 /// \macro LLVM_NO_SANITIZE
436 /// \brief Disable a particular sanitizer for a function.
437 #if __has_attribute(no_sanitize)
438 #define LLVM_NO_SANITIZE(KIND) __attribute__((no_sanitize(KIND)))
439 #else
440 #define LLVM_NO_SANITIZE(KIND)
441 #endif
442 
443 /// \brief Mark debug helper function definitions like dump() that should not be
444 /// stripped from debug builds.
445 // FIXME: Move this to a private config.h as it's not usable in public headers.
446 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
447 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
448 #else
449 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
450 #endif
451 
452 /// \macro LLVM_THREAD_LOCAL
453 /// \brief A thread-local storage specifier which can be used with globals,
454 /// extern globals, and static globals.
455 ///
456 /// This is essentially an extremely restricted analog to C++11's thread_local
457 /// support, and uses that when available. However, it falls back on
458 /// platform-specific or vendor-provided extensions when necessary. These
459 /// extensions don't support many of the C++11 thread_local's features. You
460 /// should only use this for PODs that you can statically initialize to
461 /// some constant value. In almost all circumstances this is most appropriate
462 /// for use with a pointer, integer, or small aggregation of pointers and
463 /// integers.
464 #if LLVM_ENABLE_THREADS
465 #if __has_feature(cxx_thread_local)
466 #define LLVM_THREAD_LOCAL thread_local
467 #elif defined(_MSC_VER)
468 // MSVC supports this with a __declspec.
469 #define LLVM_THREAD_LOCAL __declspec(thread)
470 #else
471 // Clang, GCC, and other compatible compilers used __thread prior to C++11 and
472 // we only need the restricted functionality that provides.
473 #define LLVM_THREAD_LOCAL __thread
474 #endif
475 #else // !LLVM_ENABLE_THREADS
476 // If threading is disabled entirely, this compiles to nothing and you get
477 // a normal global variable.
478 #define LLVM_THREAD_LOCAL
479 #endif
480 
481 #endif
482