CompilerEnvironmentClang.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. // This defines the compiler environment for clang based compilers. Please make sure to define all required features
  3. // (see VerifyCompilerEnvironment.h for reference)
  4. #if defined(__cplusplus) && __cplusplus < 201103L
  5. #error "Baselib requires C++11 support"
  6. #endif
  7. #define COMPILER_CLANG 1
  8. #define HAS_CLANG_FEATURE(x) (__has_feature(x))
  9. #define COMPILER_SUPPORTS_EXCEPTIONS HAS_CLANG_FEATURE(cxx_exceptions)
  10. #define COMPILER_SUPPORTS_RTTI HAS_CLANG_FEATURE(cxx_rtti)
  11. #define COMPILER_SUPPORTS_GENERIC_LAMBDA_EXPRESSIONS HAS_CLANG_FEATURE(cxx_generic_lambdas) // Clang >=3.4
  12. #define COMPILER_BUILTIN_EXPECT(X_, Y_) __builtin_expect((X_), (Y_))
  13. // Tells the compiler to assume that this statement is never reached.
  14. // (reaching it anyways is undefined behavior!)
  15. #define COMPILER_BUILTIN_UNREACHABLE() __builtin_unreachable()
  16. // Tells the compiler to assume that the given expression is true until the expression is modified.
  17. // (it is undefined behavior if the expression is not true after all)
  18. //
  19. // Note: There is __builtin_assume which according to documentation should be closer to what want here.
  20. // Performance tests have shown though that it behaves differently. See case 1094654
  21. #define COMPILER_BUILTIN_ASSUME(EXPR_) PP_WRAP_CODE(if (!(EXPR_)) COMPILER_BUILTIN_UNREACHABLE())
  22. #define COMPILER_NOINLINE __attribute__((unused, noinline)) // unused is needed to avoid warning when a function is not used
  23. #define COMPILER_INLINE __attribute__((unused)) inline
  24. #define COMPILER_FORCEINLINE __attribute__((unused, always_inline, nodebug)) inline
  25. #define COMPILER_EMPTYINLINE __attribute__((const, always_inline, nodebug)) inline
  26. #define COMPILER_NORETURN __attribute__((noreturn))
  27. #if __has_extension(attribute_deprecated_with_message)
  28. #define COMPILER_DEPRECATED(msg) __attribute__((deprecated(msg)))
  29. #if __has_extension(enumerator_attributes)
  30. #define COMPILER_DEPRECATED_ENUM_VALUE(msg) __attribute__((deprecated(msg)))
  31. #else
  32. #define COMPILER_DEPRECATED_ENUM_VALUE(msg)
  33. #endif
  34. #else
  35. #define COMPILER_DEPRECATED(msg) __attribute__((deprecated))
  36. #if __has_extension(enumerator_attributes)
  37. #define COMPILER_DEPRECATED_ENUM_VALUE(msg) __attribute__((deprecated))
  38. #else
  39. #define COMPILER_DEPRECATED_ENUM_VALUE(msg)
  40. #endif
  41. #endif
  42. #define COMPILER_ALIGN_OF(TYPE_) __alignof__(TYPE_)
  43. #define COMPILER_ALIGN_AS(ALIGN_) __attribute__((aligned(ALIGN_)))
  44. #define COMPILER_C_STATIC_ASSERT(EXPR_, MSG_) _Static_assert(EXPR_, MSG_)
  45. #define COMPILER_ATTRIBUTE_UNUSED __attribute__((unused))
  46. #define COMPILER_DEBUG_TRAP() __builtin_debugtrap()
  47. #define COMPILER_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  48. // Warning management
  49. // pragma message on clang does always generate a warning that cannot be disabled, therefore the clang version
  50. // of COMPILER_PRINT_MESSAGE() does nothing
  51. #define COMPILER_PRINT_MESSAGE(MESSAGE_)
  52. #define COMPILER_PRINT_WARNING(MESSAGE_) _Pragma(PP_STRINGIZE(message(__FILE__ "warning: " MESSAGE_)))
  53. #define COMPILER_WARNING_UNUSED_VARIABLE PP_STRINGIZE(-Wunused-variable)
  54. #define COMPILER_WARNING_DEPRECATED PP_STRINGIZE(-Wdeprecated)
  55. #define COMPILER_WARNINGS_PUSH _Pragma(PP_STRINGIZE(clang diagnostic push))
  56. #define COMPILER_WARNINGS_POP _Pragma(PP_STRINGIZE(clang diagnostic pop))
  57. #define COMPILER_WARNINGS_DISABLE(Warn) _Pragma(PP_STRINGIZE(clang diagnostic ignored Warn))