// This header defines the (DP)CHECK_EQ etc. macros. // // (DP)CHECK_EQ(x, y) is similar to (DP)CHECK(x == y) but will also log the // values of x and y if the condition doesn't hold. This works for basic types // and types with an operator<< or .ToString() method. // // The operands are evaluated exactly once, and even in build modes where e.g. // DCHECK is disabled, the operands and their stringification methods are still // referenced to avoid warnings about unused variables or functions. // // To support the stringification of the check operands, this header is // *significantly* larger than base/check.h, so it should be avoided in common // headers. // // This header also provides the (DP)CHECK macros (by including check.h), so if // you use e.g. both CHECK_EQ and CHECK, including this header is enough. If you // only use CHECK however, please include the smaller check.h instead.
namespace logging {
// Functions for turning check operand values into NUL-terminated C strings. // Caller takes ownership of the result and must release it with `free`. // This would normally be defined by <ostream>, but this header tries to avoid // including <ostream> to reduce compile-time. See https://crrev.com/c/2128112.
BASE_EXPORT char* CheckOpValueStr(int v);
BASE_EXPORT char* CheckOpValueStr(unsigned v);
BASE_EXPORT char* CheckOpValueStr(long v);
BASE_EXPORT char* CheckOpValueStr(unsignedlong v);
BASE_EXPORT char* CheckOpValueStr(longlong v);
BASE_EXPORT char* CheckOpValueStr(unsignedlonglong v);
BASE_EXPORT char* CheckOpValueStr(constvoid* v);
BASE_EXPORT char* CheckOpValueStr(std::nullptr_t v);
BASE_EXPORT char* CheckOpValueStr(double v); // Although the standard defines operator<< for std::string and std::string_view // in their respective headers, libc++ requires <ostream> for them. See // https://github.com/llvm/llvm-project/issues/61070. So we define non-<ostream> // versions here too.
BASE_EXPORT char* CheckOpValueStr(const std::string& v);
BASE_EXPORT char* CheckOpValueStr(std::string_view v);
// Convert a streamable value to string out-of-line to avoid <sstream>.
BASE_EXPORT char* StreamValToStr(constvoid* v, void (*stream_func)(std::ostream&, constvoid*));
template <typename T> inlinetypename std::enable_if<
base::internal::SupportsOstreamOperator<const T&>::value &&
!std::is_function<typename std::remove_pointer<T>::type>::value, char*>::type
CheckOpValueStr(const T& v) { auto f = [](std::ostream& s, constvoid* p) {
s << *reinterpret_cast<const T*>(p);
};
// operator& might be overloaded, so do the std::addressof dance. // __builtin_addressof is preferred since it also handles Obj-C ARC pointers. // Some casting is still needed, because T might be volatile. #if SUPPORTS_BUILTIN_ADDRESSOF constvoid* vp = const_cast<constvoid*>( reinterpret_cast<constvolatilevoid*>(__builtin_addressof(v))); #else constvoid* vp = reinterpret_cast<constvoid*>( const_cast<constchar*>(&reinterpret_cast<constvolatilechar&>(v))); #endif return StreamValToStr(vp, f);
}
#undef SUPPORTS_BUILTIN_ADDRESSOF
// Overload for types that have no operator<< but do have .ToString() defined. template <typename T> inlinetypename std::enable_if<
!base::internal::SupportsOstreamOperator<const T&>::value &&
base::internal::SupportsToString<const T&>::value, char*>::type
CheckOpValueStr(const T& v) { // .ToString() may not return a std::string, e.g. blink::WTF::String. return CheckOpValueStr(v.ToString());
}
// Provide an overload for functions and function pointers. Function pointers // don't implicitly convert to void* but do implicitly convert to bool, so // without this function pointers are always printed as 1 or 0. (MSVC isn't // standards-conforming here and converts function pointers to regular // pointers, so this is a no-op for MSVC.) template <typename T> inlinetypename std::enable_if<
std::is_function<typename std::remove_pointer<T>::type>::value, char*>::type
CheckOpValueStr(const T& v) { return CheckOpValueStr(reinterpret_cast<constvoid*>(v));
}
// We need overloads for enums that don't support operator<<. // (i.e. scoped enums where no operator<< overload was declared). template <typename T> inlinetypename std::enable_if<
!base::internal::SupportsOstreamOperator<const T&>::value &&
std::is_enum<T>::value, char*>::type
CheckOpValueStr(const T& v) { return CheckOpValueStr( static_cast<typename std::underlying_type<T>::type>(v));
}
// Takes ownership of `v1_str` and `v2_str`, destroying them with free(). For // use with CheckOpValueStr() which allocates these strings using strdup(). // Returns allocated string (with strdup) for passing into // ::logging::CheckError::(D)CheckOp methods. // TODO(pbos): Annotate this ABSL_ATTRIBUTE_RETURNS_NONNULL after solving // compile failure.
BASE_EXPORT char* CreateCheckOpLogMessageString(constchar* expr_str, char* v1_str, char* v2_str);
// Helper macro for binary operators. // The 'switch' is used to prevent the 'else' from being ambiguous when the // macro is used in an 'if' clause such as: // if (a == 1) // CHECK_EQ(2, a); #define CHECK_OP_FUNCTION_IMPL(check_failure_function, name, op, val1, val2) \ switch (0) \ case0: \ default: \ if (char* const message_on_fail = ::logging::Check##name##Impl( \
(val1), (val2), #val1" "#op" "#val2); \
!message_on_fail) \
; \ else \
check_failure_function(message_on_fail)
// Don't do any evaluation but still reference the same stuff as when enabled. #define DCHECK_OP(name, op, val1, val2) \
EAT_CHECK_STREAM_PARAMS((::logging::CheckOpValueStr(val1), \
::logging::CheckOpValueStr(val2), (val1)op(val2)))
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.