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