CompilerEnvironmentGcc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #pragma once
  2. // Verify that the GCC is correctly defining __cplusplus. This is not the case for
  3. // GCC versions < 4.7, where it is just defined to 1. We only error here in case of linux build
  4. // as we, as we use the commandline --std=xxx to select the featureset there. On armcc and cxppc
  5. // we do not use any >C99 features, so the detection works correctly with __cplusplus==1
  6. #if (__cplusplus == 1) && defined(LINUX)
  7. #error "This version of GCC is not supported. Please update to a more recent one."
  8. #endif
  9. #if defined(__cplusplus) && __cplusplus < 201103L
  10. #error "Baselib requires C++11 support"
  11. #endif
  12. #define COMPILER_GCC 1
  13. // __cpp_exceptions is the correct way to check whether exceptions are enabled or not, but is unfortunately not supported
  14. // by GCC versions before 5.0. For Pre 5.0 GCC, we also need to check the __EXCEPTIONS macro
  15. #if defined(__cpp_exceptions) || __EXCEPTIONS == 1
  16. #define COMPILER_SUPPORTS_EXCEPTIONS 1
  17. #else
  18. #define COMPILER_SUPPORTS_EXCEPTIONS 0
  19. #endif
  20. // __cpp_rtti is the correct way to check whether RTTI is enabled or not, but is unfortunately not supported
  21. // by GCC versions before 5.0. For Pre 5.0 GCC, we also need to check the __GXX_RTTI macro
  22. #if defined(__cpp_rtti) || __GXX_RTTI == 1
  23. #define COMPILER_SUPPORTS_RTTI 1
  24. #else
  25. #define COMPILER_SUPPORTS_RTTI 0
  26. #endif
  27. // GCC >=4.9
  28. #if defined(__cpp_generic_lambdas) && (__cpp_generic_lambdas >= 201304)
  29. #define COMPILER_SUPPORTS_GENERIC_LAMBDA_EXPRESSIONS 1
  30. #else
  31. #define COMPILER_SUPPORTS_GENERIC_LAMBDA_EXPRESSIONS 0
  32. #endif
  33. #define COMPILER_BUILTIN_EXPECT(X_, Y_) __builtin_expect((X_), (Y_))
  34. // Tells the compiler to assume that this statement is never reached.
  35. // (reaching it anyways is undefined behavior!)
  36. #define COMPILER_BUILTIN_UNREACHABLE() __builtin_unreachable()
  37. // Tells the compiler to assume that the given expression is true until the expression is modified.
  38. // (it is undefined behavior if the expression is not true after all)
  39. #define COMPILER_BUILTIN_ASSUME(EXPR_) do { if (!(EXPR_)) COMPILER_BUILTIN_UNREACHABLE(); } while(false)
  40. #define COMPILER_NOINLINE __attribute__((unused, noinline)) // unused is needed to avoid warning when a function is not used
  41. #define COMPILER_INLINE __attribute__((unused)) inline
  42. #define COMPILER_FORCEINLINE __attribute__((unused, always_inline)) inline
  43. #define COMPILER_EMPTYINLINE __attribute__((const, always_inline)) inline
  44. #define COMPILER_NORETURN __attribute__((noreturn))
  45. #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) || __GNUC__ > 4
  46. #define COMPILER_DEPRECATED(msg) __attribute__((deprecated(msg)))
  47. #else
  48. #define COMPILER_DEPRECATED(msg) __attribute__((deprecated))
  49. #endif
  50. // Support for attributes on enumerators is GCC 6
  51. #if __GNUC__ >= 6
  52. #define COMPILER_DEPRECATED_ENUM_VALUE(msg) __attribute__((deprecated(msg)))
  53. #else
  54. #define COMPILER_DEPRECATED_ENUM_VALUE(msg)
  55. #endif
  56. #define COMPILER_ALIGN_OF(TYPE_) __alignof__(TYPE_)
  57. #define COMPILER_ALIGN_AS(ALIGN_) __attribute__((aligned(ALIGN_)))
  58. #define COMPILER_C_STATIC_ASSERT(EXPR_, MSG_) _Static_assert(EXPR_, MSG_)
  59. #define COMPILER_ATTRIBUTE_UNUSED __attribute__((unused))
  60. // Some versions of GCC do provide __builtin_debugtrap, but it seems to be unreliable.
  61. // See https://github.com/scottt/debugbreak/issues/13
  62. #if defined(__i386__) || defined(__x86_64__)
  63. #define COMPILER_DEBUG_TRAP() __asm__ volatile("int $0x03")
  64. #elif defined(__thumb__)
  65. #define COMPILER_DEBUG_TRAP() __asm__ volatile(".inst 0xde01")
  66. #elif defined(__arm__) && !defined(__thumb__)
  67. #define COMPILER_DEBUG_TRAP() __asm__ volatile(".inst 0xe7f001f0")
  68. #elif defined(__aarch64__)
  69. #define COMPILER_DEBUG_TRAP() __asm__ volatile(".inst 0xd4200000")
  70. #endif
  71. #define COMPILER_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  72. #define HAS_CLANG_FEATURE(x) 0
  73. // Warning management
  74. #define COMPILER_PRINT_MESSAGE(MESSAGE_) _Pragma(PP_STRINGIZE(message(__FILE__ "info: " MESSAGE_)))
  75. #define COMPILER_PRINT_WARNING(MESSAGE_) _Pragma(PP_STRINGIZE(message(__FILE__ "warning: " MESSAGE_)))
  76. #define COMPILER_WARNING_UNUSED_VARIABLE PP_STRINGIZE(-Wunused-variable)
  77. #define COMPILER_WARNING_DEPRECATED PP_STRINGIZE(-Wdeprecated)
  78. #define COMPILER_WARNINGS_PUSH _Pragma("GCC diagnostic push")
  79. #define COMPILER_WARNINGS_POP _Pragma("GCC diagnostic pop")
  80. #define COMPILER_WARNINGS_DISABLE(Warn) _Pragma(PP_STRINGIZE(GCC diagnostic ignored Warn))
  81. // Prefetches memory for reading from address `address` if supported on the current architecture
  82. #define COMPILER_PREFETCH_READ(address) __builtin_prefetch(address, 0)
  83. // Prefetches memory for writing from address `address` if supported on the current architecture
  84. #define COMPILER_PREFETCH_WRITE(address) __builtin_prefetch(address, 1)