macros.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Copyright 2017 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // -----------------------------------------------------------------------------
  17. // File: macros.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This header file defines the set of language macros used within Abseil code.
  21. // For the set of macros used to determine supported compilers and platforms,
  22. // see y_absl/base/config.h instead.
  23. //
  24. // This code is compiled directly on many platforms, including client
  25. // platforms like Windows, Mac, and embedded systems. Before making
  26. // any changes here, make sure that you're not breaking any platforms.
  27. #ifndef Y_ABSL_BASE_MACROS_H_
  28. #define Y_ABSL_BASE_MACROS_H_
  29. #include <cassert>
  30. #include <cstddef>
  31. #include "y_absl/base/attributes.h"
  32. #include "y_absl/base/config.h"
  33. #include "y_absl/base/optimization.h"
  34. #include "y_absl/base/port.h"
  35. // Y_ABSL_ARRAYSIZE()
  36. //
  37. // Returns the number of elements in an array as a compile-time constant, which
  38. // can be used in defining new arrays. If you use this macro on a pointer by
  39. // mistake, you will get a compile-time error.
  40. #define Y_ABSL_ARRAYSIZE(array) \
  41. (sizeof(::y_absl::macros_internal::ArraySizeHelper(array)))
  42. namespace y_absl {
  43. Y_ABSL_NAMESPACE_BEGIN
  44. namespace macros_internal {
  45. // Note: this internal template function declaration is used by Y_ABSL_ARRAYSIZE.
  46. // The function doesn't need a definition, as we only use its type.
  47. template <typename T, size_t N>
  48. auto ArraySizeHelper(const T (&array)[N]) -> char (&)[N];
  49. } // namespace macros_internal
  50. Y_ABSL_NAMESPACE_END
  51. } // namespace y_absl
  52. // Y_ABSL_BAD_CALL_IF()
  53. //
  54. // Used on a function overload to trap bad calls: any call that matches the
  55. // overload will cause a compile-time error. This macro uses a clang-specific
  56. // "enable_if" attribute, as described at
  57. // https://clang.llvm.org/docs/AttributeReference.html#enable-if
  58. //
  59. // Overloads which use this macro should be bracketed by
  60. // `#ifdef Y_ABSL_BAD_CALL_IF`.
  61. //
  62. // Example:
  63. //
  64. // int isdigit(int c);
  65. // #ifdef Y_ABSL_BAD_CALL_IF
  66. // int isdigit(int c)
  67. // Y_ABSL_BAD_CALL_IF(c <= -1 || c > 255,
  68. // "'c' must have the value of an unsigned char or EOF");
  69. // #endif // Y_ABSL_BAD_CALL_IF
  70. #if Y_ABSL_HAVE_ATTRIBUTE(enable_if)
  71. #define Y_ABSL_BAD_CALL_IF(expr, msg) \
  72. __attribute__((enable_if(expr, "Bad call trap"), unavailable(msg)))
  73. #endif
  74. // Y_ABSL_ASSERT()
  75. //
  76. // In C++11, `assert` can't be used portably within constexpr functions.
  77. // Y_ABSL_ASSERT functions as a runtime assert but works in C++11 constexpr
  78. // functions. Example:
  79. //
  80. // constexpr double Divide(double a, double b) {
  81. // return Y_ABSL_ASSERT(b != 0), a / b;
  82. // }
  83. //
  84. // This macro is inspired by
  85. // https://akrzemi1.wordpress.com/2017/05/18/asserts-in-constexpr-functions/
  86. #if defined(NDEBUG)
  87. #define Y_ABSL_ASSERT(expr) \
  88. (false ? static_cast<void>(expr) : static_cast<void>(0))
  89. #else
  90. #define Y_ABSL_ASSERT(expr) \
  91. (Y_ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
  92. : [] { assert(false && #expr); }()) // NOLINT
  93. #endif
  94. // `Y_ABSL_INTERNAL_HARDENING_ABORT()` controls how `Y_ABSL_HARDENING_ASSERT()`
  95. // aborts the program in release mode (when NDEBUG is defined). The
  96. // implementation should abort the program as quickly as possible and ideally it
  97. // should not be possible to ignore the abort request.
  98. #define Y_ABSL_INTERNAL_HARDENING_ABORT() \
  99. do { \
  100. Y_ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL(); \
  101. Y_ABSL_INTERNAL_UNREACHABLE_IMPL(); \
  102. } while (false)
  103. // Y_ABSL_HARDENING_ASSERT()
  104. //
  105. // `Y_ABSL_HARDENING_ASSERT()` is like `Y_ABSL_ASSERT()`, but used to implement
  106. // runtime assertions that should be enabled in hardened builds even when
  107. // `NDEBUG` is defined.
  108. //
  109. // When `NDEBUG` is not defined, `Y_ABSL_HARDENING_ASSERT()` is identical to
  110. // `Y_ABSL_ASSERT()`.
  111. //
  112. // See `Y_ABSL_OPTION_HARDENED` in `y_absl/base/options.h` for more information on
  113. // hardened mode.
  114. #if Y_ABSL_OPTION_HARDENED == 1 && defined(NDEBUG)
  115. #define Y_ABSL_HARDENING_ASSERT(expr) \
  116. (Y_ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
  117. : [] { Y_ABSL_INTERNAL_HARDENING_ABORT(); }())
  118. #else
  119. #define Y_ABSL_HARDENING_ASSERT(expr) Y_ABSL_ASSERT(expr)
  120. #endif
  121. #ifdef Y_ABSL_HAVE_EXCEPTIONS
  122. #define Y_ABSL_INTERNAL_TRY try
  123. #define Y_ABSL_INTERNAL_CATCH_ANY catch (...)
  124. #define Y_ABSL_INTERNAL_RETHROW do { throw; } while (false)
  125. #else // Y_ABSL_HAVE_EXCEPTIONS
  126. #define Y_ABSL_INTERNAL_TRY if (true)
  127. #define Y_ABSL_INTERNAL_CATCH_ANY else if (false)
  128. #define Y_ABSL_INTERNAL_RETHROW do {} while (false)
  129. #endif // Y_ABSL_HAVE_EXCEPTIONS
  130. #endif // Y_ABSL_BASE_MACROS_H_