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