check_op.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2022 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/log/internal/check_op.h"
  15. #include <cstring>
  16. #include <ostream>
  17. #include <string>
  18. #include <utility>
  19. #include "absl/base/config.h"
  20. #include "absl/base/nullability.h"
  21. #include "absl/debugging/leak_check.h"
  22. #include "absl/strings/str_cat.h"
  23. #include "absl/strings/string_view.h"
  24. #ifdef _MSC_VER
  25. #define strcasecmp _stricmp
  26. #else
  27. #include <strings.h> // for strcasecmp, but msvc does not have this header
  28. #endif
  29. namespace absl {
  30. ABSL_NAMESPACE_BEGIN
  31. namespace log_internal {
  32. #define ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(x) \
  33. template absl::Nonnull<const char*> MakeCheckOpString( \
  34. x, x, absl::Nonnull<const char*>)
  35. ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(bool);
  36. ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(int64_t);
  37. ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(uint64_t);
  38. ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(float);
  39. ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(double);
  40. ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(char);
  41. ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(unsigned char);
  42. ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const std::string&);
  43. ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const absl::string_view&);
  44. ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const char*);
  45. ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const signed char*);
  46. ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const unsigned char*);
  47. ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const void*);
  48. #undef ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING
  49. CheckOpMessageBuilder::CheckOpMessageBuilder(
  50. absl::Nonnull<const char*> exprtext) {
  51. stream_ << exprtext << " (";
  52. }
  53. std::ostream& CheckOpMessageBuilder::ForVar2() {
  54. stream_ << " vs. ";
  55. return stream_;
  56. }
  57. absl::Nonnull<const char*> CheckOpMessageBuilder::NewString() {
  58. stream_ << ")";
  59. // There's no need to free this string since the process is crashing.
  60. return absl::IgnoreLeak(new std::string(std::move(stream_).str()))->c_str();
  61. }
  62. void MakeCheckOpValueString(std::ostream& os, const char v) {
  63. if (v >= 32 && v <= 126) {
  64. os << "'" << v << "'";
  65. } else {
  66. os << "char value " << int{v};
  67. }
  68. }
  69. void MakeCheckOpValueString(std::ostream& os, const signed char v) {
  70. if (v >= 32 && v <= 126) {
  71. os << "'" << v << "'";
  72. } else {
  73. os << "signed char value " << int{v};
  74. }
  75. }
  76. void MakeCheckOpValueString(std::ostream& os, const unsigned char v) {
  77. if (v >= 32 && v <= 126) {
  78. os << "'" << v << "'";
  79. } else {
  80. os << "unsigned char value " << int{v};
  81. }
  82. }
  83. void MakeCheckOpValueString(std::ostream& os, const void* p) {
  84. if (p == nullptr) {
  85. os << "(null)";
  86. } else {
  87. os << p;
  88. }
  89. }
  90. // Helper functions for string comparisons.
  91. #define DEFINE_CHECK_STROP_IMPL(name, func, expected) \
  92. absl::Nullable<const char*> Check##func##expected##Impl( \
  93. absl::Nullable<const char*> s1, absl::Nullable<const char*> s2, \
  94. absl::Nonnull<const char*> exprtext) { \
  95. bool equal = s1 == s2 || (s1 && s2 && !func(s1, s2)); \
  96. if (equal == expected) { \
  97. return nullptr; \
  98. } else { \
  99. /* There's no need to free this string since the process is crashing. */ \
  100. return absl::IgnoreLeak(new std::string(absl::StrCat(exprtext, " (", s1, \
  101. " vs. ", s2, ")"))) \
  102. ->c_str(); \
  103. } \
  104. }
  105. DEFINE_CHECK_STROP_IMPL(CHECK_STREQ, strcmp, true)
  106. DEFINE_CHECK_STROP_IMPL(CHECK_STRNE, strcmp, false)
  107. DEFINE_CHECK_STROP_IMPL(CHECK_STRCASEEQ, strcasecmp, true)
  108. DEFINE_CHECK_STROP_IMPL(CHECK_STRCASENE, strcasecmp, false)
  109. #undef DEFINE_CHECK_STROP_IMPL
  110. namespace detect_specialization {
  111. StringifySink::StringifySink(std::ostream& os) : os_(os) {}
  112. void StringifySink::Append(absl::string_view text) { os_ << text; }
  113. void StringifySink::Append(size_t length, char ch) {
  114. for (size_t i = 0; i < length; ++i) os_.put(ch);
  115. }
  116. void AbslFormatFlush(StringifySink* sink, absl::string_view text) {
  117. sink->Append(text);
  118. }
  119. } // namespace detect_specialization
  120. } // namespace log_internal
  121. ABSL_NAMESPACE_END
  122. } // namespace absl