Coverage Report

Created: 2024-09-23 06:29

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