LLVM  mainline
Compiler.h
Go to the documentation of this file.
00001 //===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines several macros, based on the current compiler.  This allows
00011 // use of compiler-specific features in a way that remains portable.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_SUPPORT_COMPILER_H
00016 #define LLVM_SUPPORT_COMPILER_H
00017 
00018 #include "llvm/Config/llvm-config.h"
00019 
00020 #ifndef __has_feature
00021 # define __has_feature(x) 0
00022 #endif
00023 
00024 #ifndef __has_extension
00025 # define __has_extension(x) 0
00026 #endif
00027 
00028 #ifndef __has_attribute
00029 # define __has_attribute(x) 0
00030 #endif
00031 
00032 #ifndef __has_builtin
00033 # define __has_builtin(x) 0
00034 #endif
00035 
00036 /// \macro LLVM_GNUC_PREREQ
00037 /// \brief Extend the default __GNUC_PREREQ even if glibc's features.h isn't
00038 /// available.
00039 #ifndef LLVM_GNUC_PREREQ
00040 # if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
00041 #  define LLVM_GNUC_PREREQ(maj, min, patch) \
00042     ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
00043      ((maj) << 20) + ((min) << 10) + (patch))
00044 # elif defined(__GNUC__) && defined(__GNUC_MINOR__)
00045 #  define LLVM_GNUC_PREREQ(maj, min, patch) \
00046     ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
00047 # else
00048 #  define LLVM_GNUC_PREREQ(maj, min, patch) 0
00049 # endif
00050 #endif
00051 
00052 /// \macro LLVM_MSC_PREREQ
00053 /// \brief Is the compiler MSVC of at least the specified version?
00054 /// The common \param version values to check for are:
00055 ///  * 1800: Microsoft Visual Studio 2013 / 12.0
00056 ///  * 1900: Microsoft Visual Studio 2015 / 14.0
00057 #ifdef _MSC_VER
00058 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
00059 
00060 // We require at least MSVC 2013.
00061 #if !LLVM_MSC_PREREQ(1800)
00062 #error LLVM requires at least MSVC 2013.
00063 #endif
00064 
00065 #else
00066 #define LLVM_MSC_PREREQ(version) 0
00067 #endif
00068 
00069 #if !defined(_MSC_VER) || defined(__clang__) || LLVM_MSC_PREREQ(1900)
00070 #define LLVM_NOEXCEPT noexcept
00071 #else
00072 #define LLVM_NOEXCEPT throw()
00073 #endif
00074 
00075 /// \brief Does the compiler support ref-qualifiers for *this?
00076 ///
00077 /// Sadly, this is separate from just rvalue reference support because GCC
00078 /// and MSVC implemented this later than everything else.
00079 #if __has_feature(cxx_rvalue_references) || LLVM_GNUC_PREREQ(4, 8, 1)
00080 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
00081 #else
00082 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
00083 #endif
00084 
00085 /// Expands to '&' if ref-qualifiers for *this are supported.
00086 ///
00087 /// This can be used to provide lvalue/rvalue overrides of member functions.
00088 /// The rvalue override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
00089 #if LLVM_HAS_RVALUE_REFERENCE_THIS
00090 #define LLVM_LVALUE_FUNCTION &
00091 #else
00092 #define LLVM_LVALUE_FUNCTION
00093 #endif
00094 
00095 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
00096 # define LLVM_CONSTEXPR constexpr
00097 #else
00098 # define LLVM_CONSTEXPR
00099 #endif
00100 
00101 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
00102 /// into a shared library, then the class should be private to the library and
00103 /// not accessible from outside it.  Can also be used to mark variables and
00104 /// functions, making them private to any shared library they are linked into.
00105 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
00106 #if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) &&              \
00107     !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
00108 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
00109 #else
00110 #define LLVM_LIBRARY_VISIBILITY
00111 #endif
00112 
00113 #if __has_attribute(sentinel) || LLVM_GNUC_PREREQ(3, 0, 0)
00114 #define LLVM_END_WITH_NULL __attribute__((sentinel))
00115 #else
00116 #define LLVM_END_WITH_NULL
00117 #endif
00118 
00119 #if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
00120 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
00121 #else
00122 #define LLVM_ATTRIBUTE_USED
00123 #endif
00124 
00125 #if __has_attribute(warn_unused_result) || LLVM_GNUC_PREREQ(3, 4, 0)
00126 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
00127 #else
00128 #define LLVM_ATTRIBUTE_UNUSED_RESULT
00129 #endif
00130 
00131 // Some compilers warn about unused functions. When a function is sometimes
00132 // used or not depending on build settings (e.g. a function only called from
00133 // within "assert"), this attribute can be used to suppress such warnings.
00134 //
00135 // However, it shouldn't be used for unused *variables*, as those have a much
00136 // more portable solution:
00137 //   (void)unused_var_name;
00138 // Prefer cast-to-void wherever it is sufficient.
00139 #if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0)
00140 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
00141 #else
00142 #define LLVM_ATTRIBUTE_UNUSED
00143 #endif
00144 
00145 // FIXME: Provide this for PE/COFF targets.
00146 #if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) &&                    \
00147     (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
00148 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
00149 #else
00150 #define LLVM_ATTRIBUTE_WEAK
00151 #endif
00152 
00153 // Prior to clang 3.2, clang did not accept any spelling of
00154 // __has_attribute(const), so assume it is supported.
00155 #if defined(__clang__) || defined(__GNUC__)
00156 // aka 'CONST' but following LLVM Conventions.
00157 #define LLVM_READNONE __attribute__((__const__))
00158 #else
00159 #define LLVM_READNONE
00160 #endif
00161 
00162 #if __has_attribute(pure) || defined(__GNUC__)
00163 // aka 'PURE' but following LLVM Conventions.
00164 #define LLVM_READONLY __attribute__((__pure__))
00165 #else
00166 #define LLVM_READONLY
00167 #endif
00168 
00169 #if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0)
00170 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
00171 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
00172 #else
00173 #define LLVM_LIKELY(EXPR) (EXPR)
00174 #define LLVM_UNLIKELY(EXPR) (EXPR)
00175 #endif
00176 
00177 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
00178 /// mark a method "not for inlining".
00179 #if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
00180 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
00181 #elif defined(_MSC_VER)
00182 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
00183 #else
00184 #define LLVM_ATTRIBUTE_NOINLINE
00185 #endif
00186 
00187 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
00188 /// so, mark a method "always inline" because it is performance sensitive. GCC
00189 /// 3.4 supported this but is buggy in various cases and produces unimplemented
00190 /// errors, just use it in GCC 4.0 and later.
00191 #if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
00192 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
00193 #elif defined(_MSC_VER)
00194 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
00195 #else
00196 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
00197 #endif
00198 
00199 #ifdef __GNUC__
00200 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
00201 #elif defined(_MSC_VER)
00202 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
00203 #else
00204 #define LLVM_ATTRIBUTE_NORETURN
00205 #endif
00206 
00207 #if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
00208 #define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
00209 #else
00210 #define LLVM_ATTRIBUTE_RETURNS_NONNULL
00211 #endif
00212 
00213 /// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a
00214 /// pointer that does not alias any other valid pointer.
00215 #ifdef __GNUC__
00216 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
00217 #elif defined(_MSC_VER)
00218 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
00219 #else
00220 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS
00221 #endif
00222 
00223 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
00224 /// pedantic diagnostics.
00225 #ifdef __GNUC__
00226 #define LLVM_EXTENSION __extension__
00227 #else
00228 #define LLVM_EXTENSION
00229 #endif
00230 
00231 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
00232 #if __has_feature(attribute_deprecated_with_message)
00233 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
00234   decl __attribute__((deprecated(message)))
00235 #elif defined(__GNUC__)
00236 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
00237   decl __attribute__((deprecated))
00238 #elif defined(_MSC_VER)
00239 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
00240   __declspec(deprecated(message)) decl
00241 #else
00242 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
00243   decl
00244 #endif
00245 
00246 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
00247 /// to an expression which states that it is undefined behavior for the
00248 /// compiler to reach this point.  Otherwise is not defined.
00249 #if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
00250 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
00251 #elif defined(_MSC_VER)
00252 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
00253 #endif
00254 
00255 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
00256 /// which causes the program to exit abnormally.
00257 #if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
00258 # define LLVM_BUILTIN_TRAP __builtin_trap()
00259 #elif defined(_MSC_VER)
00260 // The __debugbreak intrinsic is supported by MSVC, does not require forward
00261 // declarations involving platform-specific typedefs (unlike RaiseException),
00262 // results in a call to vectored exception handlers, and encodes to a short
00263 // instruction that still causes the trapping behavior we want.
00264 # define LLVM_BUILTIN_TRAP __debugbreak()
00265 #else
00266 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
00267 #endif
00268 
00269 /// \macro LLVM_ASSUME_ALIGNED
00270 /// \brief Returns a pointer with an assumed alignment.
00271 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
00272 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
00273 #elif defined(LLVM_BUILTIN_UNREACHABLE)
00274 // As of today, clang does not support __builtin_assume_aligned.
00275 # define LLVM_ASSUME_ALIGNED(p, a) \
00276            (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
00277 #else
00278 # define LLVM_ASSUME_ALIGNED(p, a) (p)
00279 #endif
00280 
00281 /// \macro LLVM_ALIGNAS
00282 /// \brief Used to specify a minimum alignment for a structure or variable. The
00283 /// alignment must be a constant integer. Use LLVM_PTR_SIZE to compute
00284 /// alignments in terms of the size of a pointer.
00285 ///
00286 /// Note that __declspec(align) has special quirks, it's not legal to pass a
00287 /// structure with __declspec(align) as a formal parameter.
00288 #ifdef _MSC_VER
00289 # define LLVM_ALIGNAS(x) __declspec(align(x))
00290 #elif __GNUC__ && !__has_feature(cxx_alignas) && !LLVM_GNUC_PREREQ(4, 8, 0)
00291 # define LLVM_ALIGNAS(x) __attribute__((aligned(x)))
00292 #else
00293 # define LLVM_ALIGNAS(x) alignas(x)
00294 #endif
00295 
00296 /// \macro LLVM_PACKED
00297 /// \brief Used to specify a packed structure.
00298 /// LLVM_PACKED(
00299 ///    struct A {
00300 ///      int i;
00301 ///      int j;
00302 ///      int k;
00303 ///      long long l;
00304 ///   });
00305 ///
00306 /// LLVM_PACKED_START
00307 /// struct B {
00308 ///   int i;
00309 ///   int j;
00310 ///   int k;
00311 ///   long long l;
00312 /// };
00313 /// LLVM_PACKED_END 
00314 #ifdef _MSC_VER
00315 # define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop))
00316 # define LLVM_PACKED_START __pragma(pack(push, 1))
00317 # define LLVM_PACKED_END   __pragma(pack(pop))
00318 #else
00319 # define LLVM_PACKED(d) d __attribute__((packed))
00320 # define LLVM_PACKED_START _Pragma("pack(push, 1)")
00321 # define LLVM_PACKED_END   _Pragma("pack(pop)")
00322 #endif
00323 
00324 /// \macro LLVM_PTR_SIZE
00325 /// \brief A constant integer equivalent to the value of sizeof(void*).
00326 /// Generally used in combination with LLVM_ALIGNAS or when doing computation in
00327 /// the preprocessor.
00328 #ifdef __SIZEOF_POINTER__
00329 # define LLVM_PTR_SIZE __SIZEOF_POINTER__
00330 #elif defined(_WIN64)
00331 # define LLVM_PTR_SIZE 8
00332 #elif defined(_WIN32)
00333 # define LLVM_PTR_SIZE 4
00334 #elif defined(_MSC_VER)
00335 # error "could not determine LLVM_PTR_SIZE as a constant int for MSVC"
00336 #else
00337 # define LLVM_PTR_SIZE sizeof(void *)
00338 #endif
00339 
00340 /// \macro LLVM_FUNCTION_NAME
00341 /// \brief Expands to __func__ on compilers which support it.  Otherwise,
00342 /// expands to a compiler-dependent replacement.
00343 #if defined(_MSC_VER)
00344 # define LLVM_FUNCTION_NAME __FUNCTION__
00345 #else
00346 # define LLVM_FUNCTION_NAME __func__
00347 #endif
00348 
00349 /// \macro LLVM_MEMORY_SANITIZER_BUILD
00350 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
00351 #if __has_feature(memory_sanitizer)
00352 # define LLVM_MEMORY_SANITIZER_BUILD 1
00353 # include <sanitizer/msan_interface.h>
00354 #else
00355 # define LLVM_MEMORY_SANITIZER_BUILD 0
00356 # define __msan_allocated_memory(p, size)
00357 # define __msan_unpoison(p, size)
00358 #endif
00359 
00360 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
00361 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
00362 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
00363 # define LLVM_ADDRESS_SANITIZER_BUILD 1
00364 # include <sanitizer/asan_interface.h>
00365 #else
00366 # define LLVM_ADDRESS_SANITIZER_BUILD 0
00367 # define __asan_poison_memory_region(p, size)
00368 # define __asan_unpoison_memory_region(p, size)
00369 #endif
00370 
00371 /// \macro LLVM_THREAD_SANITIZER_BUILD
00372 /// \brief Whether LLVM itself is built with ThreadSanitizer instrumentation.
00373 #if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
00374 # define LLVM_THREAD_SANITIZER_BUILD 1
00375 #else
00376 # define LLVM_THREAD_SANITIZER_BUILD 0
00377 #endif
00378 
00379 #if LLVM_THREAD_SANITIZER_BUILD
00380 // Thread Sanitizer is a tool that finds races in code.
00381 // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
00382 // tsan detects these exact functions by name.
00383 extern "C" {
00384 void AnnotateHappensAfter(const char *file, int line, const volatile void *cv);
00385 void AnnotateHappensBefore(const char *file, int line, const volatile void *cv);
00386 void AnnotateIgnoreWritesBegin(const char *file, int line);
00387 void AnnotateIgnoreWritesEnd(const char *file, int line);
00388 }
00389 
00390 // This marker is used to define a happens-before arc. The race detector will
00391 // infer an arc from the begin to the end when they share the same pointer
00392 // argument.
00393 # define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv)
00394 
00395 // This marker defines the destination of a happens-before arc.
00396 # define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv)
00397 
00398 // Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
00399 # define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
00400 
00401 // Resume checking for racy writes.
00402 # define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
00403 #else
00404 # define TsanHappensBefore(cv)
00405 # define TsanHappensAfter(cv)
00406 # define TsanIgnoreWritesBegin()
00407 # define TsanIgnoreWritesEnd()
00408 #endif
00409 
00410 /// \brief Mark debug helper function definitions like dump() that should not be
00411 /// stripped from debug builds.
00412 // FIXME: Move this to a private config.h as it's not usable in public headers.
00413 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
00414 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
00415 #else
00416 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
00417 #endif
00418 
00419 /// \macro LLVM_THREAD_LOCAL
00420 /// \brief A thread-local storage specifier which can be used with globals,
00421 /// extern globals, and static globals.
00422 ///
00423 /// This is essentially an extremely restricted analog to C++11's thread_local
00424 /// support, and uses that when available. However, it falls back on
00425 /// platform-specific or vendor-provided extensions when necessary. These
00426 /// extensions don't support many of the C++11 thread_local's features. You
00427 /// should only use this for PODs that you can statically initialize to
00428 /// some constant value. In almost all circumstances this is most appropriate
00429 /// for use with a pointer, integer, or small aggregation of pointers and
00430 /// integers.
00431 #if LLVM_ENABLE_THREADS
00432 #if __has_feature(cxx_thread_local)
00433 #define LLVM_THREAD_LOCAL thread_local
00434 #elif defined(_MSC_VER)
00435 // MSVC supports this with a __declspec.
00436 #define LLVM_THREAD_LOCAL __declspec(thread)
00437 #else
00438 // Clang, GCC, and other compatible compilers used __thread prior to C++11 and
00439 // we only need the restricted functionality that provides.
00440 #define LLVM_THREAD_LOCAL __thread
00441 #endif
00442 #else // !LLVM_ENABLE_THREADS
00443 // If threading is disabled entirely, this compiles to nothing and you get
00444 // a normal global variable.
00445 #define LLVM_THREAD_LOCAL
00446 #endif
00447 
00448 #endif