check_op.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_CHECK_OP_H_
  5. #define BASE_CHECK_OP_H_
  6. #include <cstddef>
  7. #include <type_traits>
  8. #include "base/check.h"
  9. #include "base/template_util.h"
  10. // This header defines the (DP)CHECK_EQ etc. macros.
  11. //
  12. // (DP)CHECK_EQ(x, y) is similar to (DP)CHECK(x == y) but will also log the
  13. // values of x and y if the condition doesn't hold. This works for basic types
  14. // and types with an operator<< or .ToString() method.
  15. //
  16. // The operands are evaluated exactly once, and even in build modes where e.g.
  17. // DCHECK is disabled, the operands and their stringification methods are still
  18. // referenced to avoid warnings about unused variables or functions.
  19. //
  20. // To support the stringification of the check operands, this header is
  21. // *significantly* larger than base/check.h, so it should be avoided in common
  22. // headers.
  23. //
  24. // This header also provides the (DP)CHECK macros (by including check.h), so if
  25. // you use e.g. both CHECK_EQ and CHECK, including this header is enough. If you
  26. // only use CHECK however, please include the smaller check.h instead.
  27. namespace logging {
  28. // Functions for turning check operand values into strings.
  29. // Caller takes ownership of the returned string.
  30. BASE_EXPORT char* CheckOpValueStr(int v);
  31. BASE_EXPORT char* CheckOpValueStr(unsigned v);
  32. BASE_EXPORT char* CheckOpValueStr(long v);
  33. BASE_EXPORT char* CheckOpValueStr(unsigned long v);
  34. BASE_EXPORT char* CheckOpValueStr(long long v);
  35. BASE_EXPORT char* CheckOpValueStr(unsigned long long v);
  36. BASE_EXPORT char* CheckOpValueStr(const void* v);
  37. BASE_EXPORT char* CheckOpValueStr(std::nullptr_t v);
  38. BASE_EXPORT char* CheckOpValueStr(double v);
  39. // Convert a streamable value to string out-of-line to avoid <sstream>.
  40. BASE_EXPORT char* StreamValToStr(const void* v,
  41. void (*stream_func)(std::ostream&,
  42. const void*));
  43. #ifdef __has_builtin
  44. #define SUPPORTS_BUILTIN_ADDRESSOF (__has_builtin(__builtin_addressof))
  45. #else
  46. #define SUPPORTS_BUILTIN_ADDRESSOF 0
  47. #endif
  48. template <typename T>
  49. inline typename std::enable_if<
  50. base::internal::SupportsOstreamOperator<const T&>::value &&
  51. !std::is_function<typename std::remove_pointer<T>::type>::value,
  52. char*>::type
  53. CheckOpValueStr(const T& v) {
  54. auto f = [](std::ostream& s, const void* p) {
  55. s << *reinterpret_cast<const T*>(p);
  56. };
  57. // operator& might be overloaded, so do the std::addressof dance.
  58. // __builtin_addressof is preferred since it also handles Obj-C ARC pointers.
  59. // Some casting is still needed, because T might be volatile.
  60. #if SUPPORTS_BUILTIN_ADDRESSOF
  61. const void* vp = const_cast<const void*>(
  62. reinterpret_cast<const volatile void*>(__builtin_addressof(v)));
  63. #else
  64. const void* vp = reinterpret_cast<const void*>(
  65. const_cast<const char*>(&reinterpret_cast<const volatile char&>(v)));
  66. #endif
  67. return StreamValToStr(vp, f);
  68. }
  69. #undef SUPPORTS_BUILTIN_ADDRESSOF
  70. // Overload for types that have no operator<< but do have .ToString() defined.
  71. template <typename T>
  72. inline typename std::enable_if<
  73. !base::internal::SupportsOstreamOperator<const T&>::value &&
  74. base::internal::SupportsToString<const T&>::value,
  75. char*>::type
  76. CheckOpValueStr(const T& v) {
  77. // .ToString() may not return a std::string, e.g. blink::WTF::String.
  78. return CheckOpValueStr(v.ToString());
  79. }
  80. // Provide an overload for functions and function pointers. Function pointers
  81. // don't implicitly convert to void* but do implicitly convert to bool, so
  82. // without this function pointers are always printed as 1 or 0. (MSVC isn't
  83. // standards-conforming here and converts function pointers to regular
  84. // pointers, so this is a no-op for MSVC.)
  85. template <typename T>
  86. inline typename std::enable_if<
  87. std::is_function<typename std::remove_pointer<T>::type>::value,
  88. char*>::type
  89. CheckOpValueStr(const T& v) {
  90. return CheckOpValueStr(reinterpret_cast<const void*>(v));
  91. }
  92. // We need overloads for enums that don't support operator<<.
  93. // (i.e. scoped enums where no operator<< overload was declared).
  94. template <typename T>
  95. inline typename std::enable_if<
  96. !base::internal::SupportsOstreamOperator<const T&>::value &&
  97. std::is_enum<T>::value,
  98. char*>::type
  99. CheckOpValueStr(const T& v) {
  100. return CheckOpValueStr(
  101. static_cast<typename std::underlying_type<T>::type>(v));
  102. }
  103. // Captures the result of a CHECK_op and facilitates testing as a boolean.
  104. class CheckOpResult {
  105. public:
  106. // An empty result signals success.
  107. constexpr CheckOpResult() {}
  108. // A non-success result. expr_str is something like "foo != bar". v1_str and
  109. // v2_str are the stringified run-time values of foo and bar. Takes ownership
  110. // of v1_str and v2_str.
  111. BASE_EXPORT CheckOpResult(const char* expr_str, char* v1_str, char* v2_str);
  112. // Returns true if the check succeeded.
  113. constexpr explicit operator bool() const { return !message_; }
  114. friend class CheckError;
  115. private:
  116. char* message_ = nullptr;
  117. };
  118. #if defined(OFFICIAL_BUILD) && defined(NDEBUG)
  119. // Discard log strings to reduce code bloat.
  120. #define CHECK_OP(name, op, val1, val2) CHECK((val1)op(val2))
  121. #else
  122. // Helper macro for binary operators.
  123. // The 'switch' is used to prevent the 'else' from being ambiguous when the
  124. // macro is used in an 'if' clause such as:
  125. // if (a == 1)
  126. // CHECK_EQ(2, a);
  127. #define CHECK_OP(name, op, val1, val2) \
  128. switch (0) \
  129. case 0: \
  130. default: \
  131. if (::logging::CheckOpResult true_if_passed = \
  132. ::logging::Check##name##Impl((val1), (val2), \
  133. #val1 " " #op " " #val2)) \
  134. ; \
  135. else \
  136. ::logging::CheckError::CheckOp(__FILE__, __LINE__, &true_if_passed) \
  137. .stream()
  138. #endif
  139. // The second overload avoids address-taking of static members for
  140. // fundamental types.
  141. #define DEFINE_CHECK_OP_IMPL(name, op) \
  142. template <typename T, typename U, \
  143. std::enable_if_t<!std::is_fundamental<T>::value || \
  144. !std::is_fundamental<U>::value, \
  145. int> = 0> \
  146. constexpr ::logging::CheckOpResult Check##name##Impl( \
  147. const T& v1, const U& v2, const char* expr_str) { \
  148. if (ANALYZER_ASSUME_TRUE(v1 op v2)) \
  149. return ::logging::CheckOpResult(); \
  150. return ::logging::CheckOpResult(expr_str, CheckOpValueStr(v1), \
  151. CheckOpValueStr(v2)); \
  152. } \
  153. template <typename T, typename U, \
  154. std::enable_if_t<std::is_fundamental<T>::value && \
  155. std::is_fundamental<U>::value, \
  156. int> = 0> \
  157. constexpr ::logging::CheckOpResult Check##name##Impl(T v1, U v2, \
  158. const char* expr_str) { \
  159. if (ANALYZER_ASSUME_TRUE(v1 op v2)) \
  160. return ::logging::CheckOpResult(); \
  161. return ::logging::CheckOpResult(expr_str, CheckOpValueStr(v1), \
  162. CheckOpValueStr(v2)); \
  163. }
  164. // clang-format off
  165. DEFINE_CHECK_OP_IMPL(EQ, ==)
  166. DEFINE_CHECK_OP_IMPL(NE, !=)
  167. DEFINE_CHECK_OP_IMPL(LE, <=)
  168. DEFINE_CHECK_OP_IMPL(LT, < )
  169. DEFINE_CHECK_OP_IMPL(GE, >=)
  170. DEFINE_CHECK_OP_IMPL(GT, > )
  171. #undef DEFINE_CHECK_OP_IMPL
  172. #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2)
  173. #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2)
  174. #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2)
  175. #define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2)
  176. #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2)
  177. #define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2)
  178. // clang-format on
  179. #if DCHECK_IS_ON()
  180. #define DCHECK_OP(name, op, val1, val2) \
  181. switch (0) \
  182. case 0: \
  183. default: \
  184. if (::logging::CheckOpResult true_if_passed = \
  185. ::logging::Check##name##Impl((val1), (val2), \
  186. #val1 " " #op " " #val2)) \
  187. ; \
  188. else \
  189. ::logging::CheckError::DCheckOp(__FILE__, __LINE__, &true_if_passed) \
  190. .stream()
  191. #else
  192. // Don't do any evaluation but still reference the same stuff as when enabled.
  193. #define DCHECK_OP(name, op, val1, val2) \
  194. EAT_CHECK_STREAM_PARAMS((::logging::CheckOpValueStr(val1), \
  195. ::logging::CheckOpValueStr(val2), (val1)op(val2)))
  196. #endif
  197. // clang-format off
  198. #define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
  199. #define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2)
  200. #define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2)
  201. #define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2)
  202. #define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2)
  203. #define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2)
  204. // clang-format on
  205. } // namespace logging
  206. #endif // BASE_CHECK_OP_H_