macros.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. ABSL_NAMESPACE_BEGIN
  44. namespace macros_internal {
  45. // Note: this internal template function declaration is used by 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. ABSL_NAMESPACE_END
  51. } // namespace absl
  52. // 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 ABSL_BAD_CALL_IF`.
  61. //
  62. // Example:
  63. //
  64. // int isdigit(int c);
  65. // #ifdef ABSL_BAD_CALL_IF
  66. // int isdigit(int c)
  67. // ABSL_BAD_CALL_IF(c <= -1 || c > 255,
  68. // "'c' must have the value of an unsigned char or EOF");
  69. // #endif // ABSL_BAD_CALL_IF
  70. #if ABSL_HAVE_ATTRIBUTE(enable_if)
  71. #define ABSL_BAD_CALL_IF(expr, msg) \
  72. __attribute__((enable_if(expr, "Bad call trap"), unavailable(msg)))
  73. #endif
  74. // ABSL_ASSERT()
  75. //
  76. // In C++11, `assert` can't be used portably within constexpr functions.
  77. // 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 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 ABSL_ASSERT(expr) \
  88. (false ? static_cast<void>(expr) : static_cast<void>(0))
  89. #else
  90. #define ABSL_ASSERT(expr) \
  91. (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
  92. : [] { assert(false && #expr); }()) // NOLINT
  93. #endif
  94. // `ABSL_INTERNAL_HARDENING_ABORT()` controls how `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. #if (ABSL_HAVE_BUILTIN(__builtin_trap) && \
  99. ABSL_HAVE_BUILTIN(__builtin_unreachable)) || \
  100. (defined(__GNUC__) && !defined(__clang__))
  101. #define ABSL_INTERNAL_HARDENING_ABORT() \
  102. do { \
  103. __builtin_trap(); \
  104. __builtin_unreachable(); \
  105. } while (false)
  106. #else
  107. #define ABSL_INTERNAL_HARDENING_ABORT() abort()
  108. #endif
  109. // ABSL_HARDENING_ASSERT()
  110. //
  111. // `ABSL_HARDENING_ASSERT()` is like `ABSL_ASSERT()`, but used to implement
  112. // runtime assertions that should be enabled in hardened builds even when
  113. // `NDEBUG` is defined.
  114. //
  115. // When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT()` is identical to
  116. // `ABSL_ASSERT()`.
  117. //
  118. // See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on
  119. // hardened mode.
  120. #if ABSL_OPTION_HARDENED == 1 && defined(NDEBUG)
  121. #define ABSL_HARDENING_ASSERT(expr) \
  122. (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
  123. : [] { 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 do { throw; } while (false)
  131. #else // ABSL_HAVE_EXCEPTIONS
  132. #define ABSL_INTERNAL_TRY if (true)
  133. #define ABSL_INTERNAL_CATCH_ANY else if (false)
  134. #define ABSL_INTERNAL_RETHROW do {} while (false)
  135. #endif // ABSL_HAVE_EXCEPTIONS
  136. #endif // ABSL_BASE_MACROS_H_