LLVM API Documentation

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 __GNUC_PREREQ
00037 /// \brief Defines __GNUC_PREREQ if glibc's features.h isn't available.
00038 #ifndef __GNUC_PREREQ
00039 # if defined(__GNUC__) && defined(__GNUC_MINOR__)
00040 #  define __GNUC_PREREQ(maj, min) \
00041     ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
00042 # else
00043 #  define __GNUC_PREREQ(maj, min) 0
00044 # endif
00045 #endif
00046 
00047 /// \macro LLVM_MSC_PREREQ
00048 /// \brief Is the compiler MSVC of at least the specified version?
00049 /// The common \param version values to check for are:
00050 ///  * 1700: Microsoft Visual Studio 2012 / 11.0
00051 ///  * 1800: Microsoft Visual Studio 2013 / 12.0
00052 #ifdef _MSC_VER
00053 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
00054 
00055 // We require at least MSVC 2012.
00056 #if !LLVM_MSC_PREREQ(1700)
00057 #error LLVM requires at least MSVC 2012.
00058 #endif
00059 
00060 #else
00061 #define LLVM_MSC_PREREQ(version) 0
00062 #endif
00063 
00064 /// \brief Does the compiler support r-value reference *this?
00065 ///
00066 /// Sadly, this is separate from just r-value reference support because GCC
00067 /// implemented everything but this thus far. No release of GCC yet has support
00068 /// for this feature so it is enabled with Clang only.
00069 /// FIXME: This should change to a version check when GCC grows support for it.
00070 #if __has_feature(cxx_rvalue_references)
00071 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
00072 #else
00073 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
00074 #endif
00075 
00076 /// \macro LLVM_HAS_VARIADIC_TEMPLATES
00077 /// \brief Does this compiler support variadic templates.
00078 ///
00079 /// Implies LLVM_HAS_RVALUE_REFERENCES and the existence of std::forward.
00080 #if __has_feature(cxx_variadic_templates) || LLVM_MSC_PREREQ(1800)
00081 # define LLVM_HAS_VARIADIC_TEMPLATES 1
00082 #else
00083 # define LLVM_HAS_VARIADIC_TEMPLATES 0
00084 #endif
00085 
00086 /// Expands to '&' if r-value references are supported.
00087 ///
00088 /// This can be used to provide l-value/r-value overrides of member functions.
00089 /// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
00090 #if LLVM_HAS_RVALUE_REFERENCE_THIS
00091 #define LLVM_LVALUE_FUNCTION &
00092 #else
00093 #define LLVM_LVALUE_FUNCTION
00094 #endif
00095 
00096 /// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it.
00097 /// Use to mark functions as uncallable. Member functions with this should
00098 /// be declared private so that some behavior is kept in C++03 mode.
00099 ///
00100 /// class DontCopy {
00101 /// private:
00102 ///   DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
00103 ///   DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
00104 /// public:
00105 ///   ...
00106 /// };
00107 #if __has_feature(cxx_deleted_functions) || \
00108     defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
00109 #define LLVM_DELETED_FUNCTION = delete
00110 #else
00111 #define LLVM_DELETED_FUNCTION
00112 #endif
00113 
00114 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
00115 # define LLVM_CONSTEXPR constexpr
00116 #else
00117 # define LLVM_CONSTEXPR
00118 #endif
00119 
00120 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
00121 /// into a shared library, then the class should be private to the library and
00122 /// not accessible from outside it.  Can also be used to mark variables and
00123 /// functions, making them private to any shared library they are linked into.
00124 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
00125 #if (__has_attribute(visibility) || __GNUC_PREREQ(4, 0)) &&                    \
00126     !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
00127 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
00128 #else
00129 #define LLVM_LIBRARY_VISIBILITY
00130 #endif
00131 
00132 #if __has_attribute(used) || __GNUC_PREREQ(3, 1)
00133 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
00134 #else
00135 #define LLVM_ATTRIBUTE_USED
00136 #endif
00137 
00138 #if __has_attribute(warn_unused_result) || __GNUC_PREREQ(3, 4)
00139 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
00140 #else
00141 #define LLVM_ATTRIBUTE_UNUSED_RESULT
00142 #endif
00143 
00144 // Some compilers warn about unused functions. When a function is sometimes
00145 // used or not depending on build settings (e.g. a function only called from
00146 // within "assert"), this attribute can be used to suppress such warnings.
00147 //
00148 // However, it shouldn't be used for unused *variables*, as those have a much
00149 // more portable solution:
00150 //   (void)unused_var_name;
00151 // Prefer cast-to-void wherever it is sufficient.
00152 #if __has_attribute(unused) || __GNUC_PREREQ(3, 1)
00153 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
00154 #else
00155 #define LLVM_ATTRIBUTE_UNUSED
00156 #endif
00157 
00158 // FIXME: Provide this for PE/COFF targets.
00159 #if (__has_attribute(weak) || __GNUC_PREREQ(4, 0)) &&                          \
00160     (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
00161 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
00162 #else
00163 #define LLVM_ATTRIBUTE_WEAK
00164 #endif
00165 
00166 // Prior to clang 3.2, clang did not accept any spelling of
00167 // __has_attribute(const), so assume it is supported.
00168 #if defined(__clang__) || defined(__GNUC__)
00169 // aka 'CONST' but following LLVM Conventions.
00170 #define LLVM_READNONE __attribute__((__const__))
00171 #else
00172 #define LLVM_READNONE
00173 #endif
00174 
00175 #if __has_attribute(pure) || defined(__GNUC__)
00176 // aka 'PURE' but following LLVM Conventions.
00177 #define LLVM_READONLY __attribute__((__pure__))
00178 #else
00179 #define LLVM_READONLY
00180 #endif
00181 
00182 #if __has_builtin(__builtin_expect) || __GNUC_PREREQ(4, 0)
00183 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
00184 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
00185 #else
00186 #define LLVM_LIKELY(EXPR) (EXPR)
00187 #define LLVM_UNLIKELY(EXPR) (EXPR)
00188 #endif
00189 
00190 // C++ doesn't support 'extern template' of template specializations.  GCC does,
00191 // but requires __extension__ before it.  In the header, use this:
00192 //   EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
00193 // in the .cpp file, use this:
00194 //   TEMPLATE_INSTANTIATION(class foo<bar>);
00195 #ifdef __GNUC__
00196 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
00197 #define TEMPLATE_INSTANTIATION(X) template X
00198 #else
00199 #define EXTERN_TEMPLATE_INSTANTIATION(X)
00200 #define TEMPLATE_INSTANTIATION(X)
00201 #endif
00202 
00203 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
00204 /// mark a method "not for inlining".
00205 #if __has_attribute(noinline) || __GNUC_PREREQ(3, 4)
00206 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
00207 #elif defined(_MSC_VER)
00208 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
00209 #else
00210 #define LLVM_ATTRIBUTE_NOINLINE
00211 #endif
00212 
00213 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
00214 /// so, mark a method "always inline" because it is performance sensitive. GCC
00215 /// 3.4 supported this but is buggy in various cases and produces unimplemented
00216 /// errors, just use it in GCC 4.0 and later.
00217 #if __has_attribute(always_inline) || __GNUC_PREREQ(4, 0)
00218 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
00219 #elif defined(_MSC_VER)
00220 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
00221 #else
00222 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
00223 #endif
00224 
00225 #ifdef __GNUC__
00226 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
00227 #elif defined(_MSC_VER)
00228 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
00229 #else
00230 #define LLVM_ATTRIBUTE_NORETURN
00231 #endif
00232 
00233 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
00234 /// pedantic diagnostics.
00235 #ifdef __GNUC__
00236 #define LLVM_EXTENSION __extension__
00237 #else
00238 #define LLVM_EXTENSION
00239 #endif
00240 
00241 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
00242 #if __has_feature(attribute_deprecated_with_message)
00243 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
00244   decl __attribute__((deprecated(message)))
00245 #elif defined(__GNUC__)
00246 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
00247   decl __attribute__((deprecated))
00248 #elif defined(_MSC_VER)
00249 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
00250   __declspec(deprecated(message)) decl
00251 #else
00252 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
00253   decl
00254 #endif
00255 
00256 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
00257 /// to an expression which states that it is undefined behavior for the
00258 /// compiler to reach this point.  Otherwise is not defined.
00259 #if __has_builtin(__builtin_unreachable) || __GNUC_PREREQ(4, 5)
00260 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
00261 #elif defined(_MSC_VER)
00262 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
00263 #endif
00264 
00265 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
00266 /// which causes the program to exit abnormally.
00267 #if __has_builtin(__builtin_trap) || __GNUC_PREREQ(4, 3)
00268 # define LLVM_BUILTIN_TRAP __builtin_trap()
00269 #else
00270 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
00271 #endif
00272 
00273 /// \macro LLVM_ASSUME_ALIGNED
00274 /// \brief Returns a pointer with an assumed alignment.
00275 #if __has_builtin(__builtin_assume_aligned) && __GNUC_PREREQ(4, 7)
00276 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
00277 #elif defined(LLVM_BUILTIN_UNREACHABLE)
00278 // As of today, clang does not support __builtin_assume_aligned.
00279 # define LLVM_ASSUME_ALIGNED(p, a) \
00280            (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
00281 #else
00282 # define LLVM_ASSUME_ALIGNED(p, a) (p)
00283 #endif
00284 
00285 /// \macro LLVM_FUNCTION_NAME
00286 /// \brief Expands to __func__ on compilers which support it.  Otherwise,
00287 /// expands to a compiler-dependent replacement.
00288 #if defined(_MSC_VER)
00289 # define LLVM_FUNCTION_NAME __FUNCTION__
00290 #else
00291 # define LLVM_FUNCTION_NAME __func__
00292 #endif
00293 
00294 /// \macro LLVM_MEMORY_SANITIZER_BUILD
00295 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
00296 #if __has_feature(memory_sanitizer)
00297 # define LLVM_MEMORY_SANITIZER_BUILD 1
00298 # include <sanitizer/msan_interface.h>
00299 #else
00300 # define LLVM_MEMORY_SANITIZER_BUILD 0
00301 # define __msan_allocated_memory(p, size)
00302 # define __msan_unpoison(p, size)
00303 #endif
00304 
00305 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
00306 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
00307 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
00308 # define LLVM_ADDRESS_SANITIZER_BUILD 1
00309 #else
00310 # define LLVM_ADDRESS_SANITIZER_BUILD 0
00311 #endif
00312 
00313 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
00314 /// \brief Is unaligned memory access fast on the host machine.
00315 ///
00316 /// Don't specialize on alignment for platforms where unaligned memory accesses
00317 /// generates the same code as aligned memory accesses for common types.
00318 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
00319     defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
00320     defined(_X86_) || defined(__i386) || defined(__i386__)
00321 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
00322 #else
00323 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
00324 #endif
00325 
00326 /// \macro LLVM_EXPLICIT
00327 /// \brief Expands to explicit on compilers which support explicit conversion
00328 /// operators. Otherwise expands to nothing.
00329 #if __has_feature(cxx_explicit_conversions) || \
00330     defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
00331 #define LLVM_EXPLICIT explicit
00332 #else
00333 #define LLVM_EXPLICIT
00334 #endif
00335 
00336 /// \brief Does the compiler support generalized initializers (using braced
00337 /// lists and std::initializer_list).  While clang may claim it supports general
00338 /// initializers, if we're using MSVC's headers, we might not have a usable
00339 /// std::initializer list type from the STL.  Disable this for now.
00340 #if __has_feature(cxx_generalized_initializers) && !defined(_MSC_VER)
00341 #define LLVM_HAS_INITIALIZER_LISTS 1
00342 #else
00343 #define LLVM_HAS_INITIALIZER_LISTS 0
00344 #endif
00345 
00346 /// \brief Mark debug helper function definitions like dump() that should not be
00347 /// stripped from debug builds.
00348 // FIXME: Move this to a private config.h as it's not usable in public headers.
00349 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
00350 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
00351 #else
00352 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
00353 #endif
00354 
00355 #endif