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