You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

macros.h 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 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 ABSL_BASE_MACROS_H_
  28. #define ABSL_BASE_MACROS_H_
  29. #include <cassert>
  30. #include <cstddef>
  31. #include "absl/base/attributes.h"
  32. #include "absl/base/config.h"
  33. #include "absl/base/optimization.h"
  34. #include "absl/base/port.h"
  35. // 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 ABSL_ARRAYSIZE(array) \
  41. (sizeof(::absl::macros_internal::ArraySizeHelper(array)))
  42. namespace absl
  43. {
  44. ABSL_NAMESPACE_BEGIN
  45. namespace macros_internal
  46. {
  47. // Note: this internal template function declaration is used by ABSL_ARRAYSIZE.
  48. // The function doesn't need a definition, as we only use its type.
  49. template<typename T, size_t N>
  50. auto ArraySizeHelper(const T (&array)[N]) -> char (&)[N];
  51. } // namespace macros_internal
  52. ABSL_NAMESPACE_END
  53. } // namespace absl
  54. // ABSL_BAD_CALL_IF()
  55. //
  56. // Used on a function overload to trap bad calls: any call that matches the
  57. // overload will cause a compile-time error. This macro uses a clang-specific
  58. // "enable_if" attribute, as described at
  59. // https://clang.llvm.org/docs/AttributeReference.html#enable-if
  60. //
  61. // Overloads which use this macro should be bracketed by
  62. // `#ifdef ABSL_BAD_CALL_IF`.
  63. //
  64. // Example:
  65. //
  66. // int isdigit(int c);
  67. // #ifdef ABSL_BAD_CALL_IF
  68. // int isdigit(int c)
  69. // ABSL_BAD_CALL_IF(c <= -1 || c > 255,
  70. // "'c' must have the value of an unsigned char or EOF");
  71. // #endif // ABSL_BAD_CALL_IF
  72. #if ABSL_HAVE_ATTRIBUTE(enable_if)
  73. #define ABSL_BAD_CALL_IF(expr, msg) \
  74. __attribute__((enable_if(expr, "Bad call trap"), unavailable(msg)))
  75. #endif
  76. // ABSL_ASSERT()
  77. //
  78. // In C++11, `assert` can't be used portably within constexpr functions.
  79. // ABSL_ASSERT functions as a runtime assert but works in C++11 constexpr
  80. // functions. Example:
  81. //
  82. // constexpr double Divide(double a, double b) {
  83. // return ABSL_ASSERT(b != 0), a / b;
  84. // }
  85. //
  86. // This macro is inspired by
  87. // https://akrzemi1.wordpress.com/2017/05/18/asserts-in-constexpr-functions/
  88. #if defined(NDEBUG)
  89. #define ABSL_ASSERT(expr) \
  90. (false ? static_cast<void>(expr) : static_cast<void>(0))
  91. #else
  92. #define ABSL_ASSERT(expr) \
  93. (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) : [] { assert(false && #expr); }()) // NOLINT
  94. #endif
  95. // `ABSL_INTERNAL_HARDENING_ABORT()` controls how `ABSL_HARDENING_ASSERT()`
  96. // aborts the program in release mode (when NDEBUG is defined). The
  97. // implementation should abort the program as quickly as possible and ideally it
  98. // should not be possible to ignore the abort request.
  99. #if (ABSL_HAVE_BUILTIN(__builtin_trap) && ABSL_HAVE_BUILTIN(__builtin_unreachable)) || \
  100. (defined(__GNUC__) && !defined(__clang__))
  101. #define ABSL_INTERNAL_HARDENING_ABORT() \
  102. do \
  103. { \
  104. __builtin_trap(); \
  105. __builtin_unreachable(); \
  106. } while (false)
  107. #else
  108. #define ABSL_INTERNAL_HARDENING_ABORT() abort()
  109. #endif
  110. // ABSL_HARDENING_ASSERT()
  111. //
  112. // `ABSL_HARDENING_ASSERT()` is like `ABSL_ASSERT()`, but used to implement
  113. // runtime assertions that should be enabled in hardened builds even when
  114. // `NDEBUG` is defined.
  115. //
  116. // When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT()` is identical to
  117. // `ABSL_ASSERT()`.
  118. //
  119. // See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on
  120. // hardened mode.
  121. #if ABSL_OPTION_HARDENED == 1 && defined(NDEBUG)
  122. #define ABSL_HARDENING_ASSERT(expr) \
  123. (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) : [] { ABSL_INTERNAL_HARDENING_ABORT(); }())
  124. #else
  125. #define ABSL_HARDENING_ASSERT(expr) ABSL_ASSERT(expr)
  126. #endif
  127. #ifdef ABSL_HAVE_EXCEPTIONS
  128. #define ABSL_INTERNAL_TRY try
  129. #define ABSL_INTERNAL_CATCH_ANY catch (...)
  130. #define ABSL_INTERNAL_RETHROW \
  131. do \
  132. { \
  133. throw; \
  134. } while (false)
  135. #else // ABSL_HAVE_EXCEPTIONS
  136. #define ABSL_INTERNAL_TRY if (true)
  137. #define ABSL_INTERNAL_CATCH_ANY else if (false)
  138. #define ABSL_INTERNAL_RETHROW \
  139. do \
  140. { \
  141. } while (false)
  142. #endif // ABSL_HAVE_EXCEPTIONS
  143. // `ABSL_INTERNAL_UNREACHABLE` is an unreachable statement. A program which
  144. // reaches one has undefined behavior, and the compiler may optimize
  145. // accordingly.
  146. #if defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_unreachable)
  147. #define ABSL_INTERNAL_UNREACHABLE __builtin_unreachable()
  148. #elif defined(_MSC_VER)
  149. #define ABSL_INTERNAL_UNREACHABLE __assume(0)
  150. #else
  151. #define ABSL_INTERNAL_UNREACHABLE
  152. #endif
  153. #endif // ABSL_BASE_MACROS_H_