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