1*9356374aSAndroid Build Coastguard Worker // Copyright 2022 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker //
15*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
16*9356374aSAndroid Build Coastguard Worker // File: log/internal/check_op.h
17*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
18*9356374aSAndroid Build Coastguard Worker //
19*9356374aSAndroid Build Coastguard Worker // This file declares helpers routines and macros used to implement `CHECK`
20*9356374aSAndroid Build Coastguard Worker // macros.
21*9356374aSAndroid Build Coastguard Worker
22*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_LOG_INTERNAL_CHECK_OP_H_
23*9356374aSAndroid Build Coastguard Worker #define ABSL_LOG_INTERNAL_CHECK_OP_H_
24*9356374aSAndroid Build Coastguard Worker
25*9356374aSAndroid Build Coastguard Worker #include <stdint.h>
26*9356374aSAndroid Build Coastguard Worker
27*9356374aSAndroid Build Coastguard Worker #include <cstddef>
28*9356374aSAndroid Build Coastguard Worker #include <ostream>
29*9356374aSAndroid Build Coastguard Worker #include <sstream>
30*9356374aSAndroid Build Coastguard Worker #include <string>
31*9356374aSAndroid Build Coastguard Worker #include <type_traits>
32*9356374aSAndroid Build Coastguard Worker #include <utility>
33*9356374aSAndroid Build Coastguard Worker
34*9356374aSAndroid Build Coastguard Worker #include "absl/base/attributes.h"
35*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
36*9356374aSAndroid Build Coastguard Worker #include "absl/base/optimization.h"
37*9356374aSAndroid Build Coastguard Worker #include "absl/log/internal/nullguard.h"
38*9356374aSAndroid Build Coastguard Worker #include "absl/log/internal/nullstream.h"
39*9356374aSAndroid Build Coastguard Worker #include "absl/log/internal/strip.h"
40*9356374aSAndroid Build Coastguard Worker #include "absl/strings/has_absl_stringify.h"
41*9356374aSAndroid Build Coastguard Worker #include "absl/strings/string_view.h"
42*9356374aSAndroid Build Coastguard Worker
43*9356374aSAndroid Build Coastguard Worker // `ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL` wraps string literals that
44*9356374aSAndroid Build Coastguard Worker // should be stripped when `ABSL_MIN_LOG_LEVEL` exceeds `kFatal`.
45*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_MIN_LOG_LEVEL
46*9356374aSAndroid Build Coastguard Worker #define ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL(literal) \
47*9356374aSAndroid Build Coastguard Worker (::absl::LogSeverity::kFatal >= \
48*9356374aSAndroid Build Coastguard Worker static_cast<::absl::LogSeverity>(ABSL_MIN_LOG_LEVEL) \
49*9356374aSAndroid Build Coastguard Worker ? (literal) \
50*9356374aSAndroid Build Coastguard Worker : "")
51*9356374aSAndroid Build Coastguard Worker #else
52*9356374aSAndroid Build Coastguard Worker #define ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL(literal) (literal)
53*9356374aSAndroid Build Coastguard Worker #endif
54*9356374aSAndroid Build Coastguard Worker
55*9356374aSAndroid Build Coastguard Worker #ifdef NDEBUG
56*9356374aSAndroid Build Coastguard Worker // `NDEBUG` is defined, so `DCHECK_EQ(x, y)` and so on do nothing. However, we
57*9356374aSAndroid Build Coastguard Worker // still want the compiler to parse `x` and `y`, because we don't want to lose
58*9356374aSAndroid Build Coastguard Worker // potentially useful errors and warnings.
59*9356374aSAndroid Build Coastguard Worker #define ABSL_LOG_INTERNAL_DCHECK_NOP(x, y) \
60*9356374aSAndroid Build Coastguard Worker while (false && ((void)(x), (void)(y), 0)) \
61*9356374aSAndroid Build Coastguard Worker ::absl::log_internal::NullStream().InternalStream()
62*9356374aSAndroid Build Coastguard Worker #endif
63*9356374aSAndroid Build Coastguard Worker
64*9356374aSAndroid Build Coastguard Worker #define ABSL_LOG_INTERNAL_CHECK_OP(name, op, val1, val1_text, val2, val2_text) \
65*9356374aSAndroid Build Coastguard Worker while (::std::string* absl_log_internal_check_op_result \
66*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_ATTRIBUTE_UNUSED_IF_STRIP_LOG = \
67*9356374aSAndroid Build Coastguard Worker ::absl::log_internal::name##Impl( \
68*9356374aSAndroid Build Coastguard Worker ::absl::log_internal::GetReferenceableValue(val1), \
69*9356374aSAndroid Build Coastguard Worker ::absl::log_internal::GetReferenceableValue(val2), \
70*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL( \
71*9356374aSAndroid Build Coastguard Worker val1_text " " #op " " val2_text))) \
72*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_CONDITION_FATAL(STATELESS, true) \
73*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_CHECK(*absl_log_internal_check_op_result).InternalStream()
74*9356374aSAndroid Build Coastguard Worker #define ABSL_LOG_INTERNAL_QCHECK_OP(name, op, val1, val1_text, val2, \
75*9356374aSAndroid Build Coastguard Worker val2_text) \
76*9356374aSAndroid Build Coastguard Worker while (::std::string* absl_log_internal_qcheck_op_result = \
77*9356374aSAndroid Build Coastguard Worker ::absl::log_internal::name##Impl( \
78*9356374aSAndroid Build Coastguard Worker ::absl::log_internal::GetReferenceableValue(val1), \
79*9356374aSAndroid Build Coastguard Worker ::absl::log_internal::GetReferenceableValue(val2), \
80*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL( \
81*9356374aSAndroid Build Coastguard Worker val1_text " " #op " " val2_text))) \
82*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_CONDITION_QFATAL(STATELESS, true) \
83*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_QCHECK(*absl_log_internal_qcheck_op_result).InternalStream()
84*9356374aSAndroid Build Coastguard Worker #define ABSL_LOG_INTERNAL_CHECK_STROP(func, op, expected, s1, s1_text, s2, \
85*9356374aSAndroid Build Coastguard Worker s2_text) \
86*9356374aSAndroid Build Coastguard Worker while (::std::string* absl_log_internal_check_strop_result = \
87*9356374aSAndroid Build Coastguard Worker ::absl::log_internal::Check##func##expected##Impl( \
88*9356374aSAndroid Build Coastguard Worker (s1), (s2), \
89*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL(s1_text " " #op \
90*9356374aSAndroid Build Coastguard Worker " " s2_text))) \
91*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_CONDITION_FATAL(STATELESS, true) \
92*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_CHECK(*absl_log_internal_check_strop_result) \
93*9356374aSAndroid Build Coastguard Worker .InternalStream()
94*9356374aSAndroid Build Coastguard Worker #define ABSL_LOG_INTERNAL_QCHECK_STROP(func, op, expected, s1, s1_text, s2, \
95*9356374aSAndroid Build Coastguard Worker s2_text) \
96*9356374aSAndroid Build Coastguard Worker while (::std::string* absl_log_internal_qcheck_strop_result = \
97*9356374aSAndroid Build Coastguard Worker ::absl::log_internal::Check##func##expected##Impl( \
98*9356374aSAndroid Build Coastguard Worker (s1), (s2), \
99*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL(s1_text " " #op \
100*9356374aSAndroid Build Coastguard Worker " " s2_text))) \
101*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_CONDITION_QFATAL(STATELESS, true) \
102*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_QCHECK(*absl_log_internal_qcheck_strop_result) \
103*9356374aSAndroid Build Coastguard Worker .InternalStream()
104*9356374aSAndroid Build Coastguard Worker // This one is tricky:
105*9356374aSAndroid Build Coastguard Worker // * We must evaluate `val` exactly once, yet we need to do two things with it:
106*9356374aSAndroid Build Coastguard Worker // evaluate `.ok()` and (sometimes) `.ToString()`.
107*9356374aSAndroid Build Coastguard Worker // * `val` might be an `absl::Status` or some `absl::StatusOr<T>`.
108*9356374aSAndroid Build Coastguard Worker // * `val` might be e.g. `ATemporary().GetStatus()`, which may return a
109*9356374aSAndroid Build Coastguard Worker // reference to a member of `ATemporary` that is only valid until the end of
110*9356374aSAndroid Build Coastguard Worker // the full expression.
111*9356374aSAndroid Build Coastguard Worker // * We don't want this file to depend on `absl::Status` `#include`s or linkage,
112*9356374aSAndroid Build Coastguard Worker // nor do we want to move the definition to status and introduce a dependency
113*9356374aSAndroid Build Coastguard Worker // in the other direction. We can be assured that callers must already have a
114*9356374aSAndroid Build Coastguard Worker // `Status` and the necessary `#include`s and linkage.
115*9356374aSAndroid Build Coastguard Worker // * Callsites should be small and fast (at least when `val.ok()`): one branch,
116*9356374aSAndroid Build Coastguard Worker // minimal stack footprint.
117*9356374aSAndroid Build Coastguard Worker // * In particular, the string concat stuff should be out-of-line and emitted
118*9356374aSAndroid Build Coastguard Worker // in only one TU to save linker input size
119*9356374aSAndroid Build Coastguard Worker // * We want the `val.ok()` check inline so static analyzers and optimizers can
120*9356374aSAndroid Build Coastguard Worker // see it.
121*9356374aSAndroid Build Coastguard Worker // * As usual, no braces so we can stream into the expansion with `operator<<`.
122*9356374aSAndroid Build Coastguard Worker // * Also as usual, it must expand to a single (partial) statement with no
123*9356374aSAndroid Build Coastguard Worker // ambiguous-else problems.
124*9356374aSAndroid Build Coastguard Worker // * When stripped by `ABSL_MIN_LOG_LEVEL`, we must discard the `<expr> is OK`
125*9356374aSAndroid Build Coastguard Worker // string literal and abort without doing any streaming. We don't need to
126*9356374aSAndroid Build Coastguard Worker // strip the call to stringify the non-ok `Status` as long as we don't log it;
127*9356374aSAndroid Build Coastguard Worker // dropping the `Status`'s message text is out of scope.
128*9356374aSAndroid Build Coastguard Worker #define ABSL_LOG_INTERNAL_CHECK_OK(val, val_text) \
129*9356374aSAndroid Build Coastguard Worker for (::std::pair<const ::absl::Status*, ::std::string*> \
130*9356374aSAndroid Build Coastguard Worker absl_log_internal_check_ok_goo; \
131*9356374aSAndroid Build Coastguard Worker absl_log_internal_check_ok_goo.first = \
132*9356374aSAndroid Build Coastguard Worker ::absl::log_internal::AsStatus(val), \
133*9356374aSAndroid Build Coastguard Worker absl_log_internal_check_ok_goo.second = \
134*9356374aSAndroid Build Coastguard Worker ABSL_PREDICT_TRUE(absl_log_internal_check_ok_goo.first->ok()) \
135*9356374aSAndroid Build Coastguard Worker ? nullptr \
136*9356374aSAndroid Build Coastguard Worker : ::absl::status_internal::MakeCheckFailString( \
137*9356374aSAndroid Build Coastguard Worker absl_log_internal_check_ok_goo.first, \
138*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL(val_text \
139*9356374aSAndroid Build Coastguard Worker " is OK")), \
140*9356374aSAndroid Build Coastguard Worker !ABSL_PREDICT_TRUE(absl_log_internal_check_ok_goo.first->ok());) \
141*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_CONDITION_FATAL(STATELESS, true) \
142*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_CHECK(*absl_log_internal_check_ok_goo.second) \
143*9356374aSAndroid Build Coastguard Worker .InternalStream()
144*9356374aSAndroid Build Coastguard Worker #define ABSL_LOG_INTERNAL_QCHECK_OK(val, val_text) \
145*9356374aSAndroid Build Coastguard Worker for (::std::pair<const ::absl::Status*, ::std::string*> \
146*9356374aSAndroid Build Coastguard Worker absl_log_internal_qcheck_ok_goo; \
147*9356374aSAndroid Build Coastguard Worker absl_log_internal_qcheck_ok_goo.first = \
148*9356374aSAndroid Build Coastguard Worker ::absl::log_internal::AsStatus(val), \
149*9356374aSAndroid Build Coastguard Worker absl_log_internal_qcheck_ok_goo.second = \
150*9356374aSAndroid Build Coastguard Worker ABSL_PREDICT_TRUE(absl_log_internal_qcheck_ok_goo.first->ok()) \
151*9356374aSAndroid Build Coastguard Worker ? nullptr \
152*9356374aSAndroid Build Coastguard Worker : ::absl::status_internal::MakeCheckFailString( \
153*9356374aSAndroid Build Coastguard Worker absl_log_internal_qcheck_ok_goo.first, \
154*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL(val_text \
155*9356374aSAndroid Build Coastguard Worker " is OK")), \
156*9356374aSAndroid Build Coastguard Worker !ABSL_PREDICT_TRUE(absl_log_internal_qcheck_ok_goo.first->ok());) \
157*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_CONDITION_QFATAL(STATELESS, true) \
158*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_QCHECK(*absl_log_internal_qcheck_ok_goo.second) \
159*9356374aSAndroid Build Coastguard Worker .InternalStream()
160*9356374aSAndroid Build Coastguard Worker
161*9356374aSAndroid Build Coastguard Worker namespace absl {
162*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
163*9356374aSAndroid Build Coastguard Worker
164*9356374aSAndroid Build Coastguard Worker class Status;
165*9356374aSAndroid Build Coastguard Worker template <typename T>
166*9356374aSAndroid Build Coastguard Worker class StatusOr;
167*9356374aSAndroid Build Coastguard Worker
168*9356374aSAndroid Build Coastguard Worker namespace status_internal {
169*9356374aSAndroid Build Coastguard Worker ABSL_ATTRIBUTE_PURE_FUNCTION std::string* MakeCheckFailString(
170*9356374aSAndroid Build Coastguard Worker const absl::Status* status, const char* prefix);
171*9356374aSAndroid Build Coastguard Worker } // namespace status_internal
172*9356374aSAndroid Build Coastguard Worker
173*9356374aSAndroid Build Coastguard Worker namespace log_internal {
174*9356374aSAndroid Build Coastguard Worker
175*9356374aSAndroid Build Coastguard Worker // Convert a Status or a StatusOr to its underlying status value.
176*9356374aSAndroid Build Coastguard Worker //
177*9356374aSAndroid Build Coastguard Worker // (This implementation does not require a dep on absl::Status to work.)
AsStatus(const absl::Status & s)178*9356374aSAndroid Build Coastguard Worker inline const absl::Status* AsStatus(const absl::Status& s) { return &s; }
179*9356374aSAndroid Build Coastguard Worker template <typename T>
AsStatus(const absl::StatusOr<T> & s)180*9356374aSAndroid Build Coastguard Worker const absl::Status* AsStatus(const absl::StatusOr<T>& s) {
181*9356374aSAndroid Build Coastguard Worker return &s.status();
182*9356374aSAndroid Build Coastguard Worker }
183*9356374aSAndroid Build Coastguard Worker
184*9356374aSAndroid Build Coastguard Worker // A helper class for formatting `expr (V1 vs. V2)` in a `CHECK_XX` statement.
185*9356374aSAndroid Build Coastguard Worker // See `MakeCheckOpString` for sample usage.
186*9356374aSAndroid Build Coastguard Worker class CheckOpMessageBuilder final {
187*9356374aSAndroid Build Coastguard Worker public:
188*9356374aSAndroid Build Coastguard Worker // Inserts `exprtext` and ` (` to the stream.
189*9356374aSAndroid Build Coastguard Worker explicit CheckOpMessageBuilder(const char* exprtext);
190*9356374aSAndroid Build Coastguard Worker ~CheckOpMessageBuilder() = default;
191*9356374aSAndroid Build Coastguard Worker // For inserting the first variable.
ForVar1()192*9356374aSAndroid Build Coastguard Worker std::ostream& ForVar1() { return stream_; }
193*9356374aSAndroid Build Coastguard Worker // For inserting the second variable (adds an intermediate ` vs. `).
194*9356374aSAndroid Build Coastguard Worker std::ostream& ForVar2();
195*9356374aSAndroid Build Coastguard Worker // Get the result (inserts the closing `)`).
196*9356374aSAndroid Build Coastguard Worker std::string* NewString();
197*9356374aSAndroid Build Coastguard Worker
198*9356374aSAndroid Build Coastguard Worker private:
199*9356374aSAndroid Build Coastguard Worker std::ostringstream stream_;
200*9356374aSAndroid Build Coastguard Worker };
201*9356374aSAndroid Build Coastguard Worker
202*9356374aSAndroid Build Coastguard Worker // This formats a value for a failing `CHECK_XX` statement. Ordinarily, it uses
203*9356374aSAndroid Build Coastguard Worker // the definition for `operator<<`, with a few special cases below.
204*9356374aSAndroid Build Coastguard Worker template <typename T>
MakeCheckOpValueString(std::ostream & os,const T & v)205*9356374aSAndroid Build Coastguard Worker inline void MakeCheckOpValueString(std::ostream& os, const T& v) {
206*9356374aSAndroid Build Coastguard Worker os << log_internal::NullGuard<T>::Guard(v);
207*9356374aSAndroid Build Coastguard Worker }
208*9356374aSAndroid Build Coastguard Worker
209*9356374aSAndroid Build Coastguard Worker // Overloads for char types provide readable values for unprintable characters.
210*9356374aSAndroid Build Coastguard Worker void MakeCheckOpValueString(std::ostream& os, char v);
211*9356374aSAndroid Build Coastguard Worker void MakeCheckOpValueString(std::ostream& os, signed char v);
212*9356374aSAndroid Build Coastguard Worker void MakeCheckOpValueString(std::ostream& os, unsigned char v);
213*9356374aSAndroid Build Coastguard Worker void MakeCheckOpValueString(std::ostream& os, const void* p);
214*9356374aSAndroid Build Coastguard Worker
215*9356374aSAndroid Build Coastguard Worker namespace detect_specialization {
216*9356374aSAndroid Build Coastguard Worker
217*9356374aSAndroid Build Coastguard Worker // MakeCheckOpString is being specialized for every T and U pair that is being
218*9356374aSAndroid Build Coastguard Worker // passed to the CHECK_op macros. However, there is a lot of redundancy in these
219*9356374aSAndroid Build Coastguard Worker // specializations that creates unnecessary library and binary bloat.
220*9356374aSAndroid Build Coastguard Worker // The number of instantiations tends to be O(n^2) because we have two
221*9356374aSAndroid Build Coastguard Worker // independent inputs. This technique works by reducing `n`.
222*9356374aSAndroid Build Coastguard Worker //
223*9356374aSAndroid Build Coastguard Worker // Most user-defined types being passed to CHECK_op end up being printed as a
224*9356374aSAndroid Build Coastguard Worker // builtin type. For example, enums tend to be implicitly converted to its
225*9356374aSAndroid Build Coastguard Worker // underlying type when calling operator<<, and pointers are printed with the
226*9356374aSAndroid Build Coastguard Worker // `const void*` overload.
227*9356374aSAndroid Build Coastguard Worker // To reduce the number of instantiations we coerce these values before calling
228*9356374aSAndroid Build Coastguard Worker // MakeCheckOpString instead of inside it.
229*9356374aSAndroid Build Coastguard Worker //
230*9356374aSAndroid Build Coastguard Worker // To detect if this coercion is needed, we duplicate all the relevant
231*9356374aSAndroid Build Coastguard Worker // operator<< overloads as specified in the standard, just in a different
232*9356374aSAndroid Build Coastguard Worker // namespace. If the call to `stream << value` becomes ambiguous, it means that
233*9356374aSAndroid Build Coastguard Worker // one of these overloads is the one selected by overload resolution. We then
234*9356374aSAndroid Build Coastguard Worker // do overload resolution again just with our overload set to see which one gets
235*9356374aSAndroid Build Coastguard Worker // selected. That tells us which type to coerce to.
236*9356374aSAndroid Build Coastguard Worker // If the augmented call was not ambiguous, it means that none of these were
237*9356374aSAndroid Build Coastguard Worker // selected and we can't coerce the input.
238*9356374aSAndroid Build Coastguard Worker //
239*9356374aSAndroid Build Coastguard Worker // As a secondary step to reduce code duplication, we promote integral types to
240*9356374aSAndroid Build Coastguard Worker // their 64-bit variant. This does not change the printed value, but reduces the
241*9356374aSAndroid Build Coastguard Worker // number of instantiations even further. Promoting an integer is very cheap at
242*9356374aSAndroid Build Coastguard Worker // the call site.
243*9356374aSAndroid Build Coastguard Worker int64_t operator<<(std::ostream&, short value); // NOLINT
244*9356374aSAndroid Build Coastguard Worker int64_t operator<<(std::ostream&, unsigned short value); // NOLINT
245*9356374aSAndroid Build Coastguard Worker int64_t operator<<(std::ostream&, int value);
246*9356374aSAndroid Build Coastguard Worker int64_t operator<<(std::ostream&, unsigned int value);
247*9356374aSAndroid Build Coastguard Worker int64_t operator<<(std::ostream&, long value); // NOLINT
248*9356374aSAndroid Build Coastguard Worker uint64_t operator<<(std::ostream&, unsigned long value); // NOLINT
249*9356374aSAndroid Build Coastguard Worker int64_t operator<<(std::ostream&, long long value); // NOLINT
250*9356374aSAndroid Build Coastguard Worker uint64_t operator<<(std::ostream&, unsigned long long value); // NOLINT
251*9356374aSAndroid Build Coastguard Worker float operator<<(std::ostream&, float value);
252*9356374aSAndroid Build Coastguard Worker double operator<<(std::ostream&, double value);
253*9356374aSAndroid Build Coastguard Worker long double operator<<(std::ostream&, long double value);
254*9356374aSAndroid Build Coastguard Worker bool operator<<(std::ostream&, bool value);
255*9356374aSAndroid Build Coastguard Worker const void* operator<<(std::ostream&, const void* value);
256*9356374aSAndroid Build Coastguard Worker const void* operator<<(std::ostream&, std::nullptr_t);
257*9356374aSAndroid Build Coastguard Worker
258*9356374aSAndroid Build Coastguard Worker // These `char` overloads are specified like this in the standard, so we have to
259*9356374aSAndroid Build Coastguard Worker // write them exactly the same to ensure the call is ambiguous.
260*9356374aSAndroid Build Coastguard Worker // If we wrote it in a different way (eg taking std::ostream instead of the
261*9356374aSAndroid Build Coastguard Worker // template) then one call might have a higher rank than the other and it would
262*9356374aSAndroid Build Coastguard Worker // not be ambiguous.
263*9356374aSAndroid Build Coastguard Worker template <typename Traits>
264*9356374aSAndroid Build Coastguard Worker char operator<<(std::basic_ostream<char, Traits>&, char);
265*9356374aSAndroid Build Coastguard Worker template <typename Traits>
266*9356374aSAndroid Build Coastguard Worker signed char operator<<(std::basic_ostream<char, Traits>&, signed char);
267*9356374aSAndroid Build Coastguard Worker template <typename Traits>
268*9356374aSAndroid Build Coastguard Worker unsigned char operator<<(std::basic_ostream<char, Traits>&, unsigned char);
269*9356374aSAndroid Build Coastguard Worker template <typename Traits>
270*9356374aSAndroid Build Coastguard Worker const char* operator<<(std::basic_ostream<char, Traits>&, const char*);
271*9356374aSAndroid Build Coastguard Worker template <typename Traits>
272*9356374aSAndroid Build Coastguard Worker const signed char* operator<<(std::basic_ostream<char, Traits>&,
273*9356374aSAndroid Build Coastguard Worker const signed char*);
274*9356374aSAndroid Build Coastguard Worker template <typename Traits>
275*9356374aSAndroid Build Coastguard Worker const unsigned char* operator<<(std::basic_ostream<char, Traits>&,
276*9356374aSAndroid Build Coastguard Worker const unsigned char*);
277*9356374aSAndroid Build Coastguard Worker
278*9356374aSAndroid Build Coastguard Worker // This overload triggers when the call is not ambiguous.
279*9356374aSAndroid Build Coastguard Worker // It means that T is being printed with some overload not on this list.
280*9356374aSAndroid Build Coastguard Worker // We keep the value as `const T&`.
281*9356374aSAndroid Build Coastguard Worker template <typename T, typename = decltype(std::declval<std::ostream&>()
282*9356374aSAndroid Build Coastguard Worker << std::declval<const T&>())>
283*9356374aSAndroid Build Coastguard Worker const T& Detect(int);
284*9356374aSAndroid Build Coastguard Worker
285*9356374aSAndroid Build Coastguard Worker // This overload triggers when the call is ambiguous.
286*9356374aSAndroid Build Coastguard Worker // It means that T is either one from this list or printed as one from this
287*9356374aSAndroid Build Coastguard Worker // list. Eg an enum that decays to `int` for printing.
288*9356374aSAndroid Build Coastguard Worker // We ask the overload set to give us the type we want to convert it to.
289*9356374aSAndroid Build Coastguard Worker template <typename T>
290*9356374aSAndroid Build Coastguard Worker decltype(detect_specialization::operator<<(std::declval<std::ostream&>(),
291*9356374aSAndroid Build Coastguard Worker std::declval<const T&>()))
292*9356374aSAndroid Build Coastguard Worker Detect(char);
293*9356374aSAndroid Build Coastguard Worker
294*9356374aSAndroid Build Coastguard Worker // A sink for AbslStringify which redirects everything to a std::ostream.
295*9356374aSAndroid Build Coastguard Worker class StringifySink {
296*9356374aSAndroid Build Coastguard Worker public:
297*9356374aSAndroid Build Coastguard Worker explicit StringifySink(std::ostream& os ABSL_ATTRIBUTE_LIFETIME_BOUND);
298*9356374aSAndroid Build Coastguard Worker
299*9356374aSAndroid Build Coastguard Worker void Append(absl::string_view text);
300*9356374aSAndroid Build Coastguard Worker void Append(size_t length, char ch);
301*9356374aSAndroid Build Coastguard Worker friend void AbslFormatFlush(StringifySink* sink, absl::string_view text);
302*9356374aSAndroid Build Coastguard Worker
303*9356374aSAndroid Build Coastguard Worker private:
304*9356374aSAndroid Build Coastguard Worker std::ostream& os_;
305*9356374aSAndroid Build Coastguard Worker };
306*9356374aSAndroid Build Coastguard Worker
307*9356374aSAndroid Build Coastguard Worker // Wraps a type implementing AbslStringify, and implements operator<<.
308*9356374aSAndroid Build Coastguard Worker template <typename T>
309*9356374aSAndroid Build Coastguard Worker class StringifyToStreamWrapper {
310*9356374aSAndroid Build Coastguard Worker public:
StringifyToStreamWrapper(const T & v ABSL_ATTRIBUTE_LIFETIME_BOUND)311*9356374aSAndroid Build Coastguard Worker explicit StringifyToStreamWrapper(const T& v ABSL_ATTRIBUTE_LIFETIME_BOUND)
312*9356374aSAndroid Build Coastguard Worker : v_(v) {}
313*9356374aSAndroid Build Coastguard Worker
314*9356374aSAndroid Build Coastguard Worker friend std::ostream& operator<<(std::ostream& os,
315*9356374aSAndroid Build Coastguard Worker const StringifyToStreamWrapper& wrapper) {
316*9356374aSAndroid Build Coastguard Worker StringifySink sink(os);
317*9356374aSAndroid Build Coastguard Worker AbslStringify(sink, wrapper.v_);
318*9356374aSAndroid Build Coastguard Worker return os;
319*9356374aSAndroid Build Coastguard Worker }
320*9356374aSAndroid Build Coastguard Worker
321*9356374aSAndroid Build Coastguard Worker private:
322*9356374aSAndroid Build Coastguard Worker const T& v_;
323*9356374aSAndroid Build Coastguard Worker };
324*9356374aSAndroid Build Coastguard Worker
325*9356374aSAndroid Build Coastguard Worker // This overload triggers when T implements AbslStringify.
326*9356374aSAndroid Build Coastguard Worker // StringifyToStreamWrapper is used to allow MakeCheckOpString to use
327*9356374aSAndroid Build Coastguard Worker // operator<<.
328*9356374aSAndroid Build Coastguard Worker template <typename T>
329*9356374aSAndroid Build Coastguard Worker std::enable_if_t<HasAbslStringify<T>::value,
330*9356374aSAndroid Build Coastguard Worker StringifyToStreamWrapper<T>>
331*9356374aSAndroid Build Coastguard Worker Detect(...); // Ellipsis has lowest preference when int passed.
332*9356374aSAndroid Build Coastguard Worker } // namespace detect_specialization
333*9356374aSAndroid Build Coastguard Worker
334*9356374aSAndroid Build Coastguard Worker template <typename T>
335*9356374aSAndroid Build Coastguard Worker using CheckOpStreamType = decltype(detect_specialization::Detect<T>(0));
336*9356374aSAndroid Build Coastguard Worker
337*9356374aSAndroid Build Coastguard Worker // Build the error message string. Specify no inlining for code size.
338*9356374aSAndroid Build Coastguard Worker template <typename T1, typename T2>
339*9356374aSAndroid Build Coastguard Worker ABSL_ATTRIBUTE_RETURNS_NONNULL std::string* MakeCheckOpString(
340*9356374aSAndroid Build Coastguard Worker T1 v1, T2 v2, const char* exprtext) ABSL_ATTRIBUTE_NOINLINE;
341*9356374aSAndroid Build Coastguard Worker
342*9356374aSAndroid Build Coastguard Worker template <typename T1, typename T2>
MakeCheckOpString(T1 v1,T2 v2,const char * exprtext)343*9356374aSAndroid Build Coastguard Worker std::string* MakeCheckOpString(T1 v1, T2 v2, const char* exprtext) {
344*9356374aSAndroid Build Coastguard Worker CheckOpMessageBuilder comb(exprtext);
345*9356374aSAndroid Build Coastguard Worker MakeCheckOpValueString(comb.ForVar1(), v1);
346*9356374aSAndroid Build Coastguard Worker MakeCheckOpValueString(comb.ForVar2(), v2);
347*9356374aSAndroid Build Coastguard Worker return comb.NewString();
348*9356374aSAndroid Build Coastguard Worker }
349*9356374aSAndroid Build Coastguard Worker
350*9356374aSAndroid Build Coastguard Worker // Add a few commonly used instantiations as extern to reduce size of objects
351*9356374aSAndroid Build Coastguard Worker // files.
352*9356374aSAndroid Build Coastguard Worker #define ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(x) \
353*9356374aSAndroid Build Coastguard Worker extern template std::string* MakeCheckOpString(x, x, const char*)
354*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(bool);
355*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(int64_t);
356*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(uint64_t);
357*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(float);
358*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(double);
359*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(char);
360*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(unsigned char);
361*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(const std::string&);
362*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(const absl::string_view&);
363*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(const char*);
364*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(const signed char*);
365*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(const unsigned char*);
366*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(const void*);
367*9356374aSAndroid Build Coastguard Worker #undef ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN
368*9356374aSAndroid Build Coastguard Worker
369*9356374aSAndroid Build Coastguard Worker // `ABSL_LOG_INTERNAL_CHECK_OP_IMPL_RESULT` skips formatting the Check_OP result
370*9356374aSAndroid Build Coastguard Worker // string iff `ABSL_MIN_LOG_LEVEL` exceeds `kFatal`, instead returning an empty
371*9356374aSAndroid Build Coastguard Worker // string.
372*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_MIN_LOG_LEVEL
373*9356374aSAndroid Build Coastguard Worker #define ABSL_LOG_INTERNAL_CHECK_OP_IMPL_RESULT(U1, U2, v1, v2, exprtext) \
374*9356374aSAndroid Build Coastguard Worker ((::absl::LogSeverity::kFatal >= \
375*9356374aSAndroid Build Coastguard Worker static_cast<::absl::LogSeverity>(ABSL_MIN_LOG_LEVEL)) \
376*9356374aSAndroid Build Coastguard Worker ? MakeCheckOpString<U1, U2>(v1, v2, exprtext) \
377*9356374aSAndroid Build Coastguard Worker : new std::string())
378*9356374aSAndroid Build Coastguard Worker #else
379*9356374aSAndroid Build Coastguard Worker #define ABSL_LOG_INTERNAL_CHECK_OP_IMPL_RESULT(U1, U2, v1, v2, exprtext) \
380*9356374aSAndroid Build Coastguard Worker MakeCheckOpString<U1, U2>(v1, v2, exprtext)
381*9356374aSAndroid Build Coastguard Worker #endif
382*9356374aSAndroid Build Coastguard Worker
383*9356374aSAndroid Build Coastguard Worker // Helper functions for `ABSL_LOG_INTERNAL_CHECK_OP` macro family. The
384*9356374aSAndroid Build Coastguard Worker // `(int, int)` override works around the issue that the compiler will not
385*9356374aSAndroid Build Coastguard Worker // instantiate the template version of the function on values of unnamed enum
386*9356374aSAndroid Build Coastguard Worker // type.
387*9356374aSAndroid Build Coastguard Worker #define ABSL_LOG_INTERNAL_CHECK_OP_IMPL(name, op) \
388*9356374aSAndroid Build Coastguard Worker template <typename T1, typename T2> \
389*9356374aSAndroid Build Coastguard Worker inline constexpr ::std::string* name##Impl(const T1& v1, const T2& v2, \
390*9356374aSAndroid Build Coastguard Worker const char* exprtext) { \
391*9356374aSAndroid Build Coastguard Worker using U1 = CheckOpStreamType<T1>; \
392*9356374aSAndroid Build Coastguard Worker using U2 = CheckOpStreamType<T2>; \
393*9356374aSAndroid Build Coastguard Worker return ABSL_PREDICT_TRUE(v1 op v2) \
394*9356374aSAndroid Build Coastguard Worker ? nullptr \
395*9356374aSAndroid Build Coastguard Worker : ABSL_LOG_INTERNAL_CHECK_OP_IMPL_RESULT(U1, U2, U1(v1), \
396*9356374aSAndroid Build Coastguard Worker U2(v2), exprtext); \
397*9356374aSAndroid Build Coastguard Worker } \
398*9356374aSAndroid Build Coastguard Worker inline constexpr ::std::string* name##Impl(int v1, int v2, \
399*9356374aSAndroid Build Coastguard Worker const char* exprtext) { \
400*9356374aSAndroid Build Coastguard Worker return name##Impl<int, int>(v1, v2, exprtext); \
401*9356374aSAndroid Build Coastguard Worker }
402*9356374aSAndroid Build Coastguard Worker
403*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_CHECK_OP_IMPL(Check_EQ, ==)
404*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_CHECK_OP_IMPL(Check_NE, !=)
405*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_CHECK_OP_IMPL(Check_LE, <=)
406*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_CHECK_OP_IMPL(Check_LT, <)
407*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_CHECK_OP_IMPL(Check_GE, >=)
408*9356374aSAndroid Build Coastguard Worker ABSL_LOG_INTERNAL_CHECK_OP_IMPL(Check_GT, >)
409*9356374aSAndroid Build Coastguard Worker #undef ABSL_LOG_INTERNAL_CHECK_OP_IMPL_RESULT
410*9356374aSAndroid Build Coastguard Worker #undef ABSL_LOG_INTERNAL_CHECK_OP_IMPL
411*9356374aSAndroid Build Coastguard Worker
412*9356374aSAndroid Build Coastguard Worker std::string* CheckstrcmptrueImpl(const char* s1, const char* s2,
413*9356374aSAndroid Build Coastguard Worker const char* exprtext);
414*9356374aSAndroid Build Coastguard Worker std::string* CheckstrcmpfalseImpl(const char* s1, const char* s2,
415*9356374aSAndroid Build Coastguard Worker const char* exprtext);
416*9356374aSAndroid Build Coastguard Worker std::string* CheckstrcasecmptrueImpl(const char* s1, const char* s2,
417*9356374aSAndroid Build Coastguard Worker const char* exprtext);
418*9356374aSAndroid Build Coastguard Worker std::string* CheckstrcasecmpfalseImpl(const char* s1, const char* s2,
419*9356374aSAndroid Build Coastguard Worker const char* exprtext);
420*9356374aSAndroid Build Coastguard Worker
421*9356374aSAndroid Build Coastguard Worker // `CHECK_EQ` and friends want to pass their arguments by reference, however
422*9356374aSAndroid Build Coastguard Worker // this winds up exposing lots of cases where people have defined and
423*9356374aSAndroid Build Coastguard Worker // initialized static const data members but never declared them (i.e. in a .cc
424*9356374aSAndroid Build Coastguard Worker // file), meaning they are not referenceable. This function avoids that problem
425*9356374aSAndroid Build Coastguard Worker // for integers (the most common cases) by overloading for every primitive
426*9356374aSAndroid Build Coastguard Worker // integer type, even the ones we discourage, and returning them by value.
427*9356374aSAndroid Build Coastguard Worker template <typename T>
GetReferenceableValue(const T & t)428*9356374aSAndroid Build Coastguard Worker inline constexpr const T& GetReferenceableValue(const T& t) {
429*9356374aSAndroid Build Coastguard Worker return t;
430*9356374aSAndroid Build Coastguard Worker }
GetReferenceableValue(char t)431*9356374aSAndroid Build Coastguard Worker inline constexpr char GetReferenceableValue(char t) { return t; }
GetReferenceableValue(unsigned char t)432*9356374aSAndroid Build Coastguard Worker inline constexpr unsigned char GetReferenceableValue(unsigned char t) {
433*9356374aSAndroid Build Coastguard Worker return t;
434*9356374aSAndroid Build Coastguard Worker }
GetReferenceableValue(signed char t)435*9356374aSAndroid Build Coastguard Worker inline constexpr signed char GetReferenceableValue(signed char t) { return t; }
GetReferenceableValue(short t)436*9356374aSAndroid Build Coastguard Worker inline constexpr short GetReferenceableValue(short t) { return t; } // NOLINT
GetReferenceableValue(unsigned short t)437*9356374aSAndroid Build Coastguard Worker inline constexpr unsigned short GetReferenceableValue( // NOLINT
438*9356374aSAndroid Build Coastguard Worker unsigned short t) { // NOLINT
439*9356374aSAndroid Build Coastguard Worker return t;
440*9356374aSAndroid Build Coastguard Worker }
GetReferenceableValue(int t)441*9356374aSAndroid Build Coastguard Worker inline constexpr int GetReferenceableValue(int t) { return t; }
GetReferenceableValue(unsigned int t)442*9356374aSAndroid Build Coastguard Worker inline constexpr unsigned int GetReferenceableValue(unsigned int t) {
443*9356374aSAndroid Build Coastguard Worker return t;
444*9356374aSAndroid Build Coastguard Worker }
GetReferenceableValue(long t)445*9356374aSAndroid Build Coastguard Worker inline constexpr long GetReferenceableValue(long t) { return t; } // NOLINT
GetReferenceableValue(unsigned long t)446*9356374aSAndroid Build Coastguard Worker inline constexpr unsigned long GetReferenceableValue( // NOLINT
447*9356374aSAndroid Build Coastguard Worker unsigned long t) { // NOLINT
448*9356374aSAndroid Build Coastguard Worker return t;
449*9356374aSAndroid Build Coastguard Worker }
GetReferenceableValue(long long t)450*9356374aSAndroid Build Coastguard Worker inline constexpr long long GetReferenceableValue(long long t) { // NOLINT
451*9356374aSAndroid Build Coastguard Worker return t;
452*9356374aSAndroid Build Coastguard Worker }
GetReferenceableValue(unsigned long long t)453*9356374aSAndroid Build Coastguard Worker inline constexpr unsigned long long GetReferenceableValue( // NOLINT
454*9356374aSAndroid Build Coastguard Worker unsigned long long t) { // NOLINT
455*9356374aSAndroid Build Coastguard Worker return t;
456*9356374aSAndroid Build Coastguard Worker }
457*9356374aSAndroid Build Coastguard Worker
458*9356374aSAndroid Build Coastguard Worker } // namespace log_internal
459*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
460*9356374aSAndroid Build Coastguard Worker } // namespace absl
461*9356374aSAndroid Build Coastguard Worker
462*9356374aSAndroid Build Coastguard Worker #endif // ABSL_LOG_INTERNAL_CHECK_OP_H_
463