summaryrefslogtreecommitdiff
path: root/base/check_op.h
blob: 080d822faa3e29e9a351686dab440781bd0aa3c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_CHECK_OP_H_
#define BASE_CHECK_OP_H_

#include <cstddef>
#include <string>
#include <string_view>
#include <type_traits>

#include "base/base_export.h"
#include "base/check.h"
#include "base/dcheck_is_on.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "base/strings/to_string.h"
#include "base/types/supports_ostream_operator.h"

// This header defines the (DP)CHECK_EQ etc. macros.
//
// (DP)CHECK_EQ(x, y) is similar to (DP)CHECK(x == y) but will also log the
// values of x and y if the condition doesn't hold. This works for basic types
// and types with an operator<< or .ToString() method.
//
// The operands are evaluated exactly once, and even in build modes where e.g.
// DCHECK is disabled, the operands and their stringification methods are still
// referenced to avoid warnings about unused variables or functions.
//
// Like (D)CHECK (D)CHECK_EQ also supports an optional base::NotFatalUntil
// parameter. See base/check.h.
//
// To support the stringification of the check operands, this header is
// *significantly* larger than base/check.h, so it should be avoided in common
// headers.
//
// This header also provides the (DP)CHECK macros (by including check.h), so if
// you use e.g. both CHECK_EQ and CHECK, including this header is enough. If you
// only use CHECK however, please include the smaller check.h instead.

namespace logging {

// Functions for turning check operand values into NUL-terminated C strings.
// Caller takes ownership of the result and must release it with `free`.
// This would normally be defined by <ostream>, but this header tries to avoid
// including <ostream> to reduce compile-time. See https://crrev.com/c/2128112.
BASE_EXPORT char* CheckOpValueStr(int v);
BASE_EXPORT char* CheckOpValueStr(unsigned v);
BASE_EXPORT char* CheckOpValueStr(long v);
BASE_EXPORT char* CheckOpValueStr(unsigned long v);
BASE_EXPORT char* CheckOpValueStr(long long v);
BASE_EXPORT char* CheckOpValueStr(unsigned long long v);
BASE_EXPORT char* CheckOpValueStr(const void* v);
BASE_EXPORT char* CheckOpValueStr(std::nullptr_t v);
BASE_EXPORT char* CheckOpValueStr(double v);
// Although the standard defines operator<< for std::string and std::string_view
// in their respective headers, libc++ requires <ostream> for them. See
// https://github.com/llvm/llvm-project/issues/61070. So we define non-<ostream>
// versions here too.
BASE_EXPORT char* CheckOpValueStr(const std::string& v);
BASE_EXPORT char* CheckOpValueStr(std::string_view v);

// Convert a streamable value to string out-of-line to avoid <sstream>.
BASE_EXPORT char* StreamValToStr(const void* v,
                                 void (*stream_func)(std::ostream&,
                                                     const void*));

#ifdef __has_builtin
#define SUPPORTS_BUILTIN_ADDRESSOF (__has_builtin(__builtin_addressof))
#else
#define SUPPORTS_BUILTIN_ADDRESSOF 0
#endif

template <typename T>
  requires(base::internal::SupportsOstreamOperator<const T&> &&
           !std::is_function_v<std::remove_pointer_t<T>>)
inline char* CheckOpValueStr(const T& v) {
  auto f = [](std::ostream& s, const void* p) {
    s << *reinterpret_cast<const T*>(p);
  };

  // operator& might be overloaded, so do the std::addressof dance.
  // __builtin_addressof is preferred since it also handles Obj-C ARC pointers.
  // Some casting is still needed, because T might be volatile.
#if SUPPORTS_BUILTIN_ADDRESSOF
  const void* vp = const_cast<const void*>(
      reinterpret_cast<const volatile void*>(__builtin_addressof(v)));
#else
  const void* vp = reinterpret_cast<const void*>(
      const_cast<const char*>(&reinterpret_cast<const volatile char&>(v)));
#endif
  return StreamValToStr(vp, f);
}

#undef SUPPORTS_BUILTIN_ADDRESSOF

// Overload for types that have no operator<< but do have .ToString() defined.
template <typename T>
  requires(!base::internal::SupportsOstreamOperator<const T&> &&
           base::internal::SupportsToString<const T&>)
inline char* CheckOpValueStr(const T& v) {
  // .ToString() may not return a std::string, e.g. blink::WTF::String.
  return CheckOpValueStr(v.ToString());
}

// Provide an overload for functions and function pointers. Function pointers
// don't implicitly convert to void* but do implicitly convert to bool, so
// without this function pointers are always printed as 1 or 0. (MSVC isn't
// standards-conforming here and converts function pointers to regular
// pointers, so this is a no-op for MSVC.)
template <typename T>
  requires(std::is_function_v<std::remove_pointer_t<T>>)
inline char* CheckOpValueStr(const T& v) {
  return CheckOpValueStr(reinterpret_cast<const void*>(v));
}

// We need overloads for enums that don't support operator<<.
// (i.e. scoped enums where no operator<< overload was declared).
template <typename T>
  requires(!base::internal::SupportsOstreamOperator<const T&> &&
           std::is_enum_v<T>)
inline char* CheckOpValueStr(const T& v) {
  return CheckOpValueStr(static_cast<std::underlying_type_t<T>>(v));
}

// Takes ownership of `v1_str` and `v2_str`, destroying them with free(). For
// use with CheckOpValueStr() which allocates these strings using strdup().
// Returns allocated string (with strdup) for passing into
// ::logging::CheckError::(D)CheckOp methods.
// TODO(pbos): Annotate this ABSL_ATTRIBUTE_RETURNS_NONNULL after solving
// compile failure.
BASE_EXPORT char* CreateCheckOpLogMessageString(const char* expr_str,
                                                char* v1_str,
                                                char* v2_str);

// Helper macro for binary operators.
// The 'switch' is used to prevent the 'else' from being ambiguous when the
// macro is used in an 'if' clause such as:
// if (a == 1)
//   CHECK_EQ(2, a);
#define CHECK_OP_FUNCTION_IMPL(check_failure_function, name, op, val1, val2, \
                               ...)                                          \
  switch (0)                                                                 \
  case 0:                                                                    \
  default:                                                                   \
    if (char* const message_on_fail = ::logging::Check##name##Impl(          \
            (val1), (val2), #val1 " " #op " " #val2);                        \
        !message_on_fail)                                                    \
      ;                                                                      \
    else                                                                     \
      check_failure_function(message_on_fail __VA_OPT__(, ) __VA_ARGS__)

#if !CHECK_WILL_STREAM()

// Discard log strings to reduce code bloat.
#define CHECK_OP(name, op, val1, val2, ...)                                \
  BASE_IF(BASE_IS_EMPTY(__VA_ARGS__), CHECK((val1)op(val2)),               \
          CHECK_OP_FUNCTION_IMPL(::logging::CheckError::CheckOp, name, op, \
                                 val1, val2, __VA_ARGS__))

#else

#define CHECK_OP(name, op, val1, val2, ...)                              \
  CHECK_OP_FUNCTION_IMPL(::logging::CheckError::CheckOp, name, op, val1, \
                         val2 __VA_OPT__(, ) __VA_ARGS__)

#endif

// The second overload avoids address-taking of static members for
// fundamental types.
#define DEFINE_CHECK_OP_IMPL(name, op)                                  \
  template <typename T, typename U>                                     \
    requires(!std::is_fundamental_v<T> || !std::is_fundamental_v<U>)    \
  constexpr char* Check##name##Impl(const T& v1, const U& v2,           \
                                    const char* expr_str) {             \
    if (LIKELY(ANALYZER_ASSUME_TRUE(v1 op v2)))                         \
      return nullptr;                                                   \
    return CreateCheckOpLogMessageString(expr_str, CheckOpValueStr(v1), \
                                         CheckOpValueStr(v2));          \
  }                                                                     \
  template <typename T, typename U>                                     \
    requires(std::is_fundamental_v<T> && std::is_fundamental_v<U>)      \
  constexpr char* Check##name##Impl(T v1, U v2, const char* expr_str) { \
    if (LIKELY(ANALYZER_ASSUME_TRUE(v1 op v2)))                         \
      return nullptr;                                                   \
    return CreateCheckOpLogMessageString(expr_str, CheckOpValueStr(v1), \
                                         CheckOpValueStr(v2));          \
  }

// clang-format off
DEFINE_CHECK_OP_IMPL(EQ, ==)
DEFINE_CHECK_OP_IMPL(NE, !=)
DEFINE_CHECK_OP_IMPL(LE, <=)
DEFINE_CHECK_OP_IMPL(LT, < )
DEFINE_CHECK_OP_IMPL(GE, >=)
DEFINE_CHECK_OP_IMPL(GT, > )
#undef DEFINE_CHECK_OP_IMPL
#define CHECK_EQ(val1, val2, ...) \
  CHECK_OP(EQ, ==, val1, val2 __VA_OPT__(, ) __VA_ARGS__)
#define CHECK_NE(val1, val2, ...) \
  CHECK_OP(NE, !=, val1, val2 __VA_OPT__(, ) __VA_ARGS__)
#define CHECK_LE(val1, val2, ...) \
  CHECK_OP(LE, <=, val1, val2 __VA_OPT__(, ) __VA_ARGS__)
#define CHECK_LT(val1, val2, ...) \
  CHECK_OP(LT, < , val1, val2 __VA_OPT__(, ) __VA_ARGS__)
#define CHECK_GE(val1, val2, ...) \
  CHECK_OP(GE, >=, val1, val2 __VA_OPT__(, ) __VA_ARGS__)
#define CHECK_GT(val1, val2, ...) \
  CHECK_OP(GT, > , val1, val2 __VA_OPT__(, ) __VA_ARGS__)
// clang-format on

#if DCHECK_IS_ON()

#define DCHECK_OP(name, op, val1, val2) \
  CHECK_OP_FUNCTION_IMPL(::logging::CheckError::DCheckOp, name, op, val1, val2)

#else

// Don't do any evaluation but still reference the same stuff as when enabled.
#define DCHECK_OP(name, op, val1, val2)                      \
  EAT_CHECK_STREAM_PARAMS((::logging::CheckOpValueStr(val1), \
                           ::logging::CheckOpValueStr(val2), (val1)op(val2)))

#endif

// clang-format off
#define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
#define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2)
#define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2)
#define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2)
#define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2)
#define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2)
// clang-format on

#define DUMP_WILL_BE_CHECK_OP(name, op, val1, val2)                          \
  CHECK_OP_FUNCTION_IMPL(::logging::CheckError::DumpWillBeCheckOp, name, op, \
                         val1, val2)

#define DUMP_WILL_BE_CHECK_EQ(val1, val2) \
  DUMP_WILL_BE_CHECK_OP(EQ, ==, val1, val2)
#define DUMP_WILL_BE_CHECK_NE(val1, val2) \
  DUMP_WILL_BE_CHECK_OP(NE, !=, val1, val2)
#define DUMP_WILL_BE_CHECK_LE(val1, val2) \
  DUMP_WILL_BE_CHECK_OP(LE, <=, val1, val2)
#define DUMP_WILL_BE_CHECK_LT(val1, val2) \
  DUMP_WILL_BE_CHECK_OP(LT, <, val1, val2)
#define DUMP_WILL_BE_CHECK_GE(val1, val2) \
  DUMP_WILL_BE_CHECK_OP(GE, >=, val1, val2)
#define DUMP_WILL_BE_CHECK_GT(val1, val2) \
  DUMP_WILL_BE_CHECK_OP(GT, >, val1, val2)

}  // namespace logging

#endif  // BASE_CHECK_OP_H_