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