Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:40:53

0001 // Copyright 2022 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // -----------------------------------------------------------------------------
0016 // File: log/check.h
0017 // -----------------------------------------------------------------------------
0018 //
0019 // This header declares a family of `CHECK` macros.
0020 //
0021 // `CHECK` macros terminate the program with a fatal error if the specified
0022 // condition is not true.
0023 //
0024 // Except for those whose names begin with `DCHECK`, these macros are not
0025 // controlled by `NDEBUG` (cf. `assert`), so the check will be executed
0026 // regardless of compilation mode. `CHECK` and friends are thus useful for
0027 // confirming invariants in situations where continuing to run would be worse
0028 // than terminating, e.g., due to risk of data corruption or security
0029 // compromise.  It is also more robust and portable to deliberately terminate
0030 // at a particular place with a useful message and backtrace than to assume some
0031 // ultimately unspecified and unreliable crashing behavior (such as a
0032 // "segmentation fault").
0033 
0034 #ifndef ABSL_LOG_CHECK_H_
0035 #define ABSL_LOG_CHECK_H_
0036 
0037 #include "absl/log/internal/check_impl.h"
0038 #include "absl/log/internal/check_op.h"     // IWYU pragma: export
0039 #include "absl/log/internal/conditions.h"   // IWYU pragma: export
0040 #include "absl/log/internal/log_message.h"  // IWYU pragma: export
0041 #include "absl/log/internal/strip.h"        // IWYU pragma: export
0042 
0043 // CHECK()
0044 //
0045 // `CHECK` terminates the program with a fatal error if `condition` is not true.
0046 //
0047 // The message may include additional information such as stack traces, when
0048 // available.
0049 //
0050 // Example:
0051 //
0052 //   CHECK(!cheese.empty()) << "Out of Cheese";
0053 //
0054 // Might produce a message like:
0055 //
0056 //   Check failed: !cheese.empty() Out of Cheese
0057 #define CHECK(condition) ABSL_LOG_INTERNAL_CHECK_IMPL((condition), #condition)
0058 
0059 // QCHECK()
0060 //
0061 // `QCHECK` behaves like `CHECK` but does not print a full stack trace and does
0062 // not run registered error handlers (as `QFATAL`).  It is useful when the
0063 // problem is definitely unrelated to program flow, e.g. when validating user
0064 // input.
0065 #define QCHECK(condition) ABSL_LOG_INTERNAL_QCHECK_IMPL((condition), #condition)
0066 
0067 // PCHECK()
0068 //
0069 // `PCHECK` behaves like `CHECK` but appends a description of the current state
0070 // of `errno` to the failure message.
0071 //
0072 // Example:
0073 //
0074 //   int fd = open("/var/empty/missing", O_RDONLY);
0075 //   PCHECK(fd != -1) << "posix is difficult";
0076 //
0077 // Might produce a message like:
0078 //
0079 //   Check failed: fd != -1 posix is difficult: No such file or directory [2]
0080 #define PCHECK(condition) ABSL_LOG_INTERNAL_PCHECK_IMPL((condition), #condition)
0081 
0082 // DCHECK()
0083 //
0084 // `DCHECK` behaves like `CHECK` in debug mode and does nothing otherwise (as
0085 // `DLOG`).  Unlike with `CHECK` (but as with `assert`), it is not safe to rely
0086 // on evaluation of `condition`: when `NDEBUG` is enabled, DCHECK does not
0087 // evaluate the condition.
0088 #define DCHECK(condition) ABSL_LOG_INTERNAL_DCHECK_IMPL((condition), #condition)
0089 
0090 // `CHECK_EQ` and friends are syntactic sugar for `CHECK(x == y)` that
0091 // automatically output the expression being tested and the evaluated values on
0092 // either side.
0093 //
0094 // Example:
0095 //
0096 //   int x = 3, y = 5;
0097 //   CHECK_EQ(2 * x, y) << "oops!";
0098 //
0099 // Might produce a message like:
0100 //
0101 //   Check failed: 2 * x == y (6 vs. 5) oops!
0102 //
0103 // The values must implement the appropriate comparison operator as well as
0104 // `operator<<(std::ostream&, ...)`.  Care is taken to ensure that each
0105 // argument is evaluated exactly once, and that anything which is legal to pass
0106 // as a function argument is legal here.  In particular, the arguments may be
0107 // temporary expressions which will end up being destroyed at the end of the
0108 // statement,
0109 //
0110 // Example:
0111 //
0112 //   CHECK_EQ(std::string("abc")[1], 'b');
0113 //
0114 // WARNING: Passing `NULL` as an argument to `CHECK_EQ` and similar macros does
0115 // not compile.  Use `nullptr` instead.
0116 #define CHECK_EQ(val1, val2) \
0117   ABSL_LOG_INTERNAL_CHECK_EQ_IMPL((val1), #val1, (val2), #val2)
0118 #define CHECK_NE(val1, val2) \
0119   ABSL_LOG_INTERNAL_CHECK_NE_IMPL((val1), #val1, (val2), #val2)
0120 #define CHECK_LE(val1, val2) \
0121   ABSL_LOG_INTERNAL_CHECK_LE_IMPL((val1), #val1, (val2), #val2)
0122 #define CHECK_LT(val1, val2) \
0123   ABSL_LOG_INTERNAL_CHECK_LT_IMPL((val1), #val1, (val2), #val2)
0124 #define CHECK_GE(val1, val2) \
0125   ABSL_LOG_INTERNAL_CHECK_GE_IMPL((val1), #val1, (val2), #val2)
0126 #define CHECK_GT(val1, val2) \
0127   ABSL_LOG_INTERNAL_CHECK_GT_IMPL((val1), #val1, (val2), #val2)
0128 #define QCHECK_EQ(val1, val2) \
0129   ABSL_LOG_INTERNAL_QCHECK_EQ_IMPL((val1), #val1, (val2), #val2)
0130 #define QCHECK_NE(val1, val2) \
0131   ABSL_LOG_INTERNAL_QCHECK_NE_IMPL((val1), #val1, (val2), #val2)
0132 #define QCHECK_LE(val1, val2) \
0133   ABSL_LOG_INTERNAL_QCHECK_LE_IMPL((val1), #val1, (val2), #val2)
0134 #define QCHECK_LT(val1, val2) \
0135   ABSL_LOG_INTERNAL_QCHECK_LT_IMPL((val1), #val1, (val2), #val2)
0136 #define QCHECK_GE(val1, val2) \
0137   ABSL_LOG_INTERNAL_QCHECK_GE_IMPL((val1), #val1, (val2), #val2)
0138 #define QCHECK_GT(val1, val2) \
0139   ABSL_LOG_INTERNAL_QCHECK_GT_IMPL((val1), #val1, (val2), #val2)
0140 #define DCHECK_EQ(val1, val2) \
0141   ABSL_LOG_INTERNAL_DCHECK_EQ_IMPL((val1), #val1, (val2), #val2)
0142 #define DCHECK_NE(val1, val2) \
0143   ABSL_LOG_INTERNAL_DCHECK_NE_IMPL((val1), #val1, (val2), #val2)
0144 #define DCHECK_LE(val1, val2) \
0145   ABSL_LOG_INTERNAL_DCHECK_LE_IMPL((val1), #val1, (val2), #val2)
0146 #define DCHECK_LT(val1, val2) \
0147   ABSL_LOG_INTERNAL_DCHECK_LT_IMPL((val1), #val1, (val2), #val2)
0148 #define DCHECK_GE(val1, val2) \
0149   ABSL_LOG_INTERNAL_DCHECK_GE_IMPL((val1), #val1, (val2), #val2)
0150 #define DCHECK_GT(val1, val2) \
0151   ABSL_LOG_INTERNAL_DCHECK_GT_IMPL((val1), #val1, (val2), #val2)
0152 
0153 // `CHECK_OK` and friends validate that the provided `absl::Status` or
0154 // `absl::StatusOr<T>` is OK.  If it isn't, they print a failure message that
0155 // includes the actual status and terminate the program.
0156 //
0157 // As with all `DCHECK` variants, `DCHECK_OK` has no effect (not even
0158 // evaluating its argument) if `NDEBUG` is enabled.
0159 //
0160 // Example:
0161 //
0162 //   CHECK_OK(FunctionReturnsStatus(x, y, z)) << "oops!";
0163 //
0164 // Might produce a message like:
0165 //
0166 //   Check failed: FunctionReturnsStatus(x, y, z) is OK (ABORTED: timeout) oops!
0167 #define CHECK_OK(status) ABSL_LOG_INTERNAL_CHECK_OK_IMPL((status), #status)
0168 #define QCHECK_OK(status) ABSL_LOG_INTERNAL_QCHECK_OK_IMPL((status), #status)
0169 #define DCHECK_OK(status) ABSL_LOG_INTERNAL_DCHECK_OK_IMPL((status), #status)
0170 
0171 // `CHECK_STREQ` and friends provide `CHECK_EQ` functionality for C strings,
0172 // i.e., null-terminated char arrays.  The `CASE` versions are case-insensitive.
0173 //
0174 // Example:
0175 //
0176 //   CHECK_STREQ(argv[0], "./skynet");
0177 //
0178 // Note that both arguments may be temporary strings which are destroyed by the
0179 // compiler at the end of the current full expression.
0180 //
0181 // Example:
0182 //
0183 //   CHECK_STREQ(Foo().c_str(), Bar().c_str());
0184 #define CHECK_STREQ(s1, s2) \
0185   ABSL_LOG_INTERNAL_CHECK_STREQ_IMPL((s1), #s1, (s2), #s2)
0186 #define CHECK_STRNE(s1, s2) \
0187   ABSL_LOG_INTERNAL_CHECK_STRNE_IMPL((s1), #s1, (s2), #s2)
0188 #define CHECK_STRCASEEQ(s1, s2) \
0189   ABSL_LOG_INTERNAL_CHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2)
0190 #define CHECK_STRCASENE(s1, s2) \
0191   ABSL_LOG_INTERNAL_CHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2)
0192 #define QCHECK_STREQ(s1, s2) \
0193   ABSL_LOG_INTERNAL_QCHECK_STREQ_IMPL((s1), #s1, (s2), #s2)
0194 #define QCHECK_STRNE(s1, s2) \
0195   ABSL_LOG_INTERNAL_QCHECK_STRNE_IMPL((s1), #s1, (s2), #s2)
0196 #define QCHECK_STRCASEEQ(s1, s2) \
0197   ABSL_LOG_INTERNAL_QCHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2)
0198 #define QCHECK_STRCASENE(s1, s2) \
0199   ABSL_LOG_INTERNAL_QCHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2)
0200 #define DCHECK_STREQ(s1, s2) \
0201   ABSL_LOG_INTERNAL_DCHECK_STREQ_IMPL((s1), #s1, (s2), #s2)
0202 #define DCHECK_STRNE(s1, s2) \
0203   ABSL_LOG_INTERNAL_DCHECK_STRNE_IMPL((s1), #s1, (s2), #s2)
0204 #define DCHECK_STRCASEEQ(s1, s2) \
0205   ABSL_LOG_INTERNAL_DCHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2)
0206 #define DCHECK_STRCASENE(s1, s2) \
0207   ABSL_LOG_INTERNAL_DCHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2)
0208 
0209 #endif  // ABSL_LOG_CHECK_H_