Coverage Report

Created: 2025-07-11 06:37

/src/abseil-cpp/absl/log/internal/check_op.cc
Line
Count
Source (jump to first uncovered line)
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
15
#include "absl/log/internal/check_op.h"
16
17
#include <cstring>
18
#include <ostream>
19
#include <string>
20
#include <utility>
21
22
#include "absl/base/config.h"
23
#include "absl/base/nullability.h"
24
#include "absl/debugging/leak_check.h"
25
#include "absl/strings/str_cat.h"
26
#include "absl/strings/string_view.h"
27
28
#ifdef _MSC_VER
29
#define strcasecmp _stricmp
30
#else
31
#include <strings.h>  // for strcasecmp, but msvc does not have this header
32
#endif
33
34
namespace absl {
35
ABSL_NAMESPACE_BEGIN
36
namespace log_internal {
37
38
#define ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(x) \
39
  template const char* absl_nonnull MakeCheckOpString(   \
40
      x, x, const char* absl_nonnull)
41
ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(bool);
42
ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(int64_t);
43
ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(uint64_t);
44
ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(float);
45
ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(double);
46
ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(char);
47
ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(unsigned char);
48
ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const std::string&);
49
ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const absl::string_view&);
50
ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const char*);
51
ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const signed char*);
52
ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const unsigned char*);
53
ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const void*);
54
#undef ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING
55
56
CheckOpMessageBuilder::CheckOpMessageBuilder(
57
0
    const char* absl_nonnull exprtext) {
58
0
  stream_ << exprtext << " (";
59
0
}
60
61
0
std::ostream& CheckOpMessageBuilder::ForVar2() {
62
0
  stream_ << " vs. ";
63
0
  return stream_;
64
0
}
65
66
0
const char* absl_nonnull CheckOpMessageBuilder::NewString() {
67
0
  stream_ << ")";
68
  // There's no need to free this string since the process is crashing.
69
0
  return absl::IgnoreLeak(new std::string(std::move(stream_).str()))->c_str();
70
0
}
71
72
0
void MakeCheckOpValueString(std::ostream& os, const char v) {
73
0
  if (v >= 32 && v <= 126) {
74
0
    os << "'" << v << "'";
75
0
  } else {
76
0
    os << "char value " << int{v};
77
0
  }
78
0
}
79
80
0
void MakeCheckOpValueString(std::ostream& os, const signed char v) {
81
0
  if (v >= 32 && v <= 126) {
82
0
    os << "'" << v << "'";
83
0
  } else {
84
0
    os << "signed char value " << int{v};
85
0
  }
86
0
}
87
88
0
void MakeCheckOpValueString(std::ostream& os, const unsigned char v) {
89
0
  if (v >= 32 && v <= 126) {
90
0
    os << "'" << v << "'";
91
0
  } else {
92
0
    os << "unsigned char value " << int{v};
93
0
  }
94
0
}
95
96
0
void MakeCheckOpValueString(std::ostream& os, const void* p) {
97
0
  if (p == nullptr) {
98
0
    os << "(null)";
99
0
  } else {
100
0
    os << p;
101
0
  }
102
0
}
103
104
// Helper functions for string comparisons.
105
#define DEFINE_CHECK_STROP_IMPL(name, func, expected)                          \
106
  const char* absl_nullable Check##func##expected##Impl(                       \
107
      const char* absl_nullable s1, const char* absl_nullable s2,              \
108
0
      const char* absl_nonnull exprtext) {                                     \
109
0
    bool equal = s1 == s2 || (s1 && s2 && !func(s1, s2));                      \
110
0
    if (equal == expected) {                                                   \
111
0
      return nullptr;                                                          \
112
0
    } else {                                                                   \
113
0
      /* There's no need to free this string since the process is crashing. */ \
114
0
      return absl::IgnoreLeak(new std::string(absl::StrCat(exprtext, " (", s1, \
115
0
                                                           " vs. ", s2, ")"))) \
116
0
          ->c_str();                                                           \
117
0
    }                                                                          \
118
0
  }
Unexecuted instantiation: absl::log_internal::CheckstrcmptrueImpl(char const*, char const*, char const*)
Unexecuted instantiation: absl::log_internal::CheckstrcmpfalseImpl(char const*, char const*, char const*)
Unexecuted instantiation: absl::log_internal::CheckstrcasecmptrueImpl(char const*, char const*, char const*)
Unexecuted instantiation: absl::log_internal::CheckstrcasecmpfalseImpl(char const*, char const*, char const*)
119
DEFINE_CHECK_STROP_IMPL(CHECK_STREQ, strcmp, true)
120
DEFINE_CHECK_STROP_IMPL(CHECK_STRNE, strcmp, false)
121
DEFINE_CHECK_STROP_IMPL(CHECK_STRCASEEQ, strcasecmp, true)
122
DEFINE_CHECK_STROP_IMPL(CHECK_STRCASENE, strcasecmp, false)
123
#undef DEFINE_CHECK_STROP_IMPL
124
125
namespace detect_specialization {
126
127
0
StringifySink::StringifySink(std::ostream& os) : os_(os) {}
128
129
0
void StringifySink::Append(absl::string_view text) { os_ << text; }
130
131
0
void StringifySink::Append(size_t length, char ch) {
132
0
  for (size_t i = 0; i < length; ++i) os_.put(ch);
133
0
}
134
135
0
void AbslFormatFlush(StringifySink* sink, absl::string_view text) {
136
0
  sink->Append(text);
137
0
}
138
139
}  // namespace detect_specialization
140
141
}  // namespace log_internal
142
ABSL_NAMESPACE_END
143
}  // namespace absl