1 //===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines several macros, based on the current compiler. This allows
11 // use of compiler-specific features in a way that remains portable.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_COMPILER_H
16 #define LLVM_SUPPORT_COMPILER_H
19 # define __has_feature(x) 0
22 /// LLVM_HAS_RVALUE_REFERENCES - Does the compiler provide r-value references?
23 /// This implies that <utility> provides the one-argument std::move; it
24 /// does not imply the existence of any other C++ library features.
25 #if (__has_feature(cxx_rvalue_references) \
26 || defined(__GXX_EXPERIMENTAL_CXX0X__) \
27 || (defined(_MSC_VER) && _MSC_VER >= 1600))
28 #define LLVM_USE_RVALUE_REFERENCES 1
30 #define LLVM_USE_RVALUE_REFERENCES 0
33 /// llvm_move - Expands to ::std::move if the compiler supports
34 /// r-value references; otherwise, expands to the argument.
35 #if LLVM_USE_RVALUE_REFERENCES
36 #define llvm_move(value) (::std::move(value))
38 #define llvm_move(value) (value)
41 /// Expands to '&' if r-value references are supported.
43 /// This can be used to provide l-value/r-value overrides of member functions.
44 /// The r-value override should be guarded by LLVM_USE_RVALUE_REFERENCES
45 #if LLVM_USE_RVALUE_REFERENCES
46 #define LLVM_LVALUE_FUNCTION &
48 #define LLVM_LVALUE_FUNCTION
51 /// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it.
52 /// Use to mark functions as uncallable. Member functions with this should
53 /// be declared private so that some behavior is kept in C++03 mode.
57 /// DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
58 /// DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
62 #if (__has_feature(cxx_deleted_functions) \
63 || defined(__GXX_EXPERIMENTAL_CXX0X__))
64 // No version of MSVC currently supports this.
65 #define LLVM_DELETED_FUNCTION = delete
67 #define LLVM_DELETED_FUNCTION
70 /// LLVM_FINAL - Expands to 'final' if the compiler supports it.
71 /// Use to mark classes or virtual methods as final.
72 #if (__has_feature(cxx_override_control))
73 #define LLVM_FINAL final
78 /// LLVM_OVERRIDE - Expands to 'override' if the compiler supports it.
79 /// Use to mark virtual methods as overriding a base class method.
80 #if (__has_feature(cxx_override_control))
81 #define LLVM_OVERRIDE override
86 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
87 /// into a shared library, then the class should be private to the library and
88 /// not accessible from outside it. Can also be used to mark variables and
89 /// functions, making them private to any shared library they are linked into.
90 #if (__GNUC__ >= 4) && !defined(__MINGW32__) && !defined(__CYGWIN__)
91 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
93 #define LLVM_LIBRARY_VISIBILITY
96 #if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
97 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
99 #define LLVM_ATTRIBUTE_USED
102 // Some compilers warn about unused functions. When a function is sometimes
103 // used or not depending on build settings (e.g. a function only called from
104 // within "assert"), this attribute can be used to suppress such warnings.
106 // However, it shouldn't be used for unused *variables*, as those have a much
107 // more portable solution:
108 // (void)unused_var_name;
109 // Prefer cast-to-void wherever it is sufficient.
110 #if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
111 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
113 #define LLVM_ATTRIBUTE_UNUSED
116 #if (__GNUC__ >= 4) && !defined(__MINGW32__) && !defined(__CYGWIN__)
117 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
119 #define LLVM_ATTRIBUTE_WEAK
122 #ifdef __GNUC__ // aka 'CONST' but following LLVM Conventions.
123 #define LLVM_READNONE __attribute__((__const__))
125 #define LLVM_READNONE
128 #ifdef __GNUC__ // aka 'PURE' but following LLVM Conventions.
129 #define LLVM_READONLY __attribute__((__pure__))
131 #define LLVM_READONLY
135 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
136 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
138 #define LLVM_LIKELY(EXPR) (EXPR)
139 #define LLVM_UNLIKELY(EXPR) (EXPR)
143 // C++ doesn't support 'extern template' of template specializations. GCC does,
144 // but requires __extension__ before it. In the header, use this:
145 // EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
146 // in the .cpp file, use this:
147 // TEMPLATE_INSTANTIATION(class foo<bar>);
149 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
150 #define TEMPLATE_INSTANTIATION(X) template X
152 #define EXTERN_TEMPLATE_INSTANTIATION(X)
153 #define TEMPLATE_INSTANTIATION(X)
156 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
157 /// mark a method "not for inlining".
158 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
159 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
160 #elif defined(_MSC_VER)
161 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
163 #define LLVM_ATTRIBUTE_NOINLINE
166 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
167 /// so, mark a method "always inline" because it is performance sensitive. GCC
168 /// 3.4 supported this but is buggy in various cases and produces unimplemented
169 /// errors, just use it in GCC 4.0 and later.
171 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
172 #elif defined(_MSC_VER)
173 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
175 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
180 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
181 #elif defined(_MSC_VER)
182 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
184 #define LLVM_ATTRIBUTE_NORETURN
187 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
188 /// pedantic diagnostics.
190 #define LLVM_EXTENSION __extension__
192 #define LLVM_EXTENSION
195 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
196 #if __has_feature(attribute_deprecated_with_message)
197 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
198 decl __attribute__((deprecated(message)))
199 #elif defined(__GNUC__)
200 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
201 decl __attribute__((deprecated))
202 #elif defined(_MSC_VER)
203 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
204 __declspec(deprecated(message)) decl
206 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
210 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
211 /// to an expression which states that it is undefined behavior for the
212 /// compiler to reach this point. Otherwise is not defined.
213 #if defined(__clang__) || (__GNUC__ > 4) \
214 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
215 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
218 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
219 /// which causes the program to exit abnormally.
220 #if defined(__clang__) || (__GNUC__ > 4) \
221 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
222 # define LLVM_BUILTIN_TRAP __builtin_trap()
224 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0