/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 <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  | 0  | std::ostream& operator<<(std::ostream& os, UnprintableWrapper) { | 
105  | 0  |   return os << "UNPRINTABLE";  | 
106  | 0  | }  | 
107  |  |  | 
108  |  | // Helper functions for string comparisons.  | 
109  |  | #define DEFINE_CHECK_STROP_IMPL(name, func, expected)                          \  | 
110  |  |   const char* absl_nullable Check##func##expected##Impl(                       \  | 
111  |  |       const char* absl_nullable s1, const char* absl_nullable s2,              \  | 
112  | 0  |       const char* absl_nonnull exprtext) {                                     \ | 
113  | 0  |     bool equal = s1 == s2 || (s1 && s2 && !func(s1, s2));                      \  | 
114  | 0  |     if (equal == expected) {                                                   \ | 
115  | 0  |       return nullptr;                                                          \  | 
116  | 0  |     } else {                                                                   \ | 
117  | 0  |       /* There's no need to free this string since the process is crashing. */ \  | 
118  | 0  |       return absl::IgnoreLeak(new std::string(absl::StrCat(exprtext, " (", s1, \ | 
119  | 0  |                                                            " vs. ", s2, ")"))) \  | 
120  | 0  |           ->c_str();                                                           \  | 
121  | 0  |     }                                                                          \  | 
122  | 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*)  | 
123  |  | DEFINE_CHECK_STROP_IMPL(CHECK_STREQ, strcmp, true)  | 
124  |  | DEFINE_CHECK_STROP_IMPL(CHECK_STRNE, strcmp, false)  | 
125  |  | DEFINE_CHECK_STROP_IMPL(CHECK_STRCASEEQ, strcasecmp, true)  | 
126  |  | DEFINE_CHECK_STROP_IMPL(CHECK_STRCASENE, strcasecmp, false)  | 
127  |  | #undef DEFINE_CHECK_STROP_IMPL  | 
128  |  |  | 
129  |  | namespace detect_specialization { | 
130  |  |  | 
131  | 0  | StringifySink::StringifySink(std::ostream& os) : os_(os) {} | 
132  |  |  | 
133  | 0  | void StringifySink::Append(absl::string_view text) { os_ << text; } | 
134  |  |  | 
135  | 0  | void StringifySink::Append(size_t length, char ch) { | 
136  | 0  |   for (size_t i = 0; i < length; ++i) os_.put(ch);  | 
137  | 0  | }  | 
138  |  |  | 
139  | 0  | void AbslFormatFlush(StringifySink* sink, absl::string_view text) { | 
140  | 0  |   sink->Append(text);  | 
141  | 0  | }  | 
142  |  |  | 
143  |  | }  // namespace detect_specialization  | 
144  |  |  | 
145  |  | }  // namespace log_internal  | 
146  |  | ABSL_NAMESPACE_END  | 
147  |  | }  // namespace absl  |