Coverage Report

Created: 2026-09-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/base/macros.h
Line
Count
Source
1
//
2
// Copyright 2017 The Abseil Authors.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
//
8
//      https://www.apache.org/licenses/LICENSE-2.0
9
//
10
// Unless required by applicable law or agreed to in writing, software
11
// distributed under the License is distributed on an "AS IS" BASIS,
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
// See the License for the specific language governing permissions and
14
// limitations under the License.
15
//
16
// -----------------------------------------------------------------------------
17
// File: macros.h
18
// -----------------------------------------------------------------------------
19
//
20
// This header file defines the set of language macros used within Abseil code.
21
// For the set of macros used to determine supported compilers and platforms,
22
// see absl/base/config.h instead.
23
//
24
// This code is compiled directly on many platforms, including client
25
// platforms like Windows, Mac, and embedded systems.  Before making
26
// any changes here, make sure that you're not breaking any platforms.
27
28
#ifndef ABSL_BASE_MACROS_H_
29
#define ABSL_BASE_MACROS_H_
30
31
#include <atomic>
32
#include <cassert>
33
#include <cstddef>
34
35
#include "absl/base/attributes.h"
36
#include "absl/base/config.h"
37
#include "absl/base/optimization.h"
38
#include "absl/base/options.h"
39
#include "absl/base/port.h"
40
41
// ABSL_ARRAYSIZE()
42
//
43
// Returns the number of elements in an array as a compile-time constant, which
44
// can be used in defining new arrays. If you use this macro on a pointer by
45
// mistake, you will get a compile-time error.
46
//
47
// NOTE: Avoid using this macro. Instead, use std::size(a) if possible, or
48
// std::extent_v<decltype(a)> otherwise.
49
#define ABSL_ARRAYSIZE(array) \
50
  (sizeof(::absl::macros_internal::ArraySizeHelper(array)))
51
52
namespace absl {
53
ABSL_NAMESPACE_BEGIN
54
namespace macros_internal {
55
// Note: this internal template function declaration is used by ABSL_ARRAYSIZE.
56
// The function doesn't need a definition, as we only use its type.
57
template <typename T, size_t N>
58
auto ArraySizeHelper(const T (&array)[N]) -> char (&)[N];
59
}  // namespace macros_internal
60
61
namespace base_internal {
62
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::nomerge)
63
[[clang::nomerge]]  // Needed when this function is not inlined
64
#endif
65
0
[[noreturn]] inline void HardeningAbort() {
66
0
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::nomerge)
67
0
  [[clang::nomerge]]  // Needed when this function is inlined
68
0
#endif
69
0
  ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL();
70
0
  ABSL_INTERNAL_UNREACHABLE_IMPL();
71
0
}
72
}  // namespace base_internal
73
ABSL_NAMESPACE_END
74
}  // namespace absl
75
76
// ABSL_INTERNAL_UNEVALUATED()
77
//
78
// Expands into a no-op expression that contains the given expression. Used to
79
// avoid unused-variable warnings in configurations that don't need to evaluate
80
// the given expression (e.g., NDEBUG).
81
#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
82
// We use `decltype` here to avoid generating unnecessary code that the
83
// optimizer then has to optimize away.
84
// This not only improves compilation performance by reducing codegen bloat
85
// and optimization work, but also guarantees fast run-time performance without
86
// having to rely on the optimizer.
87
#define ABSL_INTERNAL_UNEVALUATED(expr) (decltype((void)(expr))())
88
#else
89
// Pre-C++20, lambdas can't be inside unevaluated operands, so we're forced to
90
// rely on the optimizer.
91
#define ABSL_INTERNAL_UNEVALUATED(expr) (false ? (void)(expr) : void())
92
#endif
93
94
// ABSL_BAD_CALL_IF()
95
//
96
// Used on a function overload to trap bad calls: any call that matches the
97
// overload will cause a compile-time error. This macro uses a clang-specific
98
// "enable_if" attribute, as described at
99
// https://clang.llvm.org/docs/AttributeReference.html#enable-if
100
//
101
// Overloads which use this macro should be bracketed by
102
// `#ifdef ABSL_BAD_CALL_IF`.
103
//
104
// Example:
105
//
106
//   int isdigit(int c);
107
//   #ifdef ABSL_BAD_CALL_IF
108
//   int isdigit(int c)
109
//     ABSL_BAD_CALL_IF(c <= -1 || c > 255,
110
//                       "'c' must have the value of an unsigned char or EOF");
111
//   #endif // ABSL_BAD_CALL_IF
112
#if ABSL_HAVE_ATTRIBUTE(enable_if)
113
#define ABSL_BAD_CALL_IF(expr, msg) \
114
  __attribute__((enable_if(expr, "Bad call trap"), unavailable(msg)))
115
#endif
116
117
// ABSL_ASSERT()
118
//
119
// `assert` generates spurious unused-symbol warnings when NDEBUG is defined.
120
// ABSL_ASSERT functions as a runtime assert but maintains references to
121
// symbols even under NDEBUG.  Example:
122
//
123
// constexpr double Divide(double a, double b) {
124
//   return ABSL_ASSERT(b != 0), a / b;
125
// }
126
//
127
// This macro is inspired by
128
// https://akrzemi1.wordpress.com/2017/05/18/asserts-in-constexpr-functions/
129
#if defined(NDEBUG)
130
#define ABSL_ASSERT(expr) ABSL_INTERNAL_UNEVALUATED((expr) ? void() : void())
131
#else
132
#define ABSL_ASSERT(expr)                           \
133
1.11M
  (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
134
1.11M
                             : assert(false && #expr))  // NOLINT
135
#endif
136
137
// `ABSL_INTERNAL_HARDENING_ABORT()` controls how `ABSL_HARDENING_ASSERT()`
138
// aborts the program in release mode (when NDEBUG is defined). The
139
// implementation should abort the program as quickly as possible and ideally it
140
// should not be possible to ignore the abort request.
141
#if defined(__CUDACC__) || defined(__CUDA_ARCH__) || defined(__CUDA__)
142
#define ABSL_INTERNAL_HARDENING_ABORT()   \
143
  do {                                    \
144
    ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL(); \
145
    ABSL_INTERNAL_UNREACHABLE_IMPL();     \
146
  } while (false)
147
#else
148
#define ABSL_INTERNAL_HARDENING_ABORT() ::absl::base_internal::HardeningAbort()
149
#endif
150
151
// ABSL_HARDENING_ASSERT()
152
//
153
// `ABSL_HARDENING_ASSERT()` is like `ABSL_ASSERT()`, but used to implement
154
// runtime assertions that should be enabled in hardened builds even when
155
// `NDEBUG` is defined.
156
//
157
// When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT()` is identical to
158
// `ABSL_ASSERT()`.
159
//
160
// See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on
161
// hardened mode.
162
#if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG)
163
#define ABSL_HARDENING_ASSERT(expr)    \
164
  do {                                 \
165
    if (!ABSL_PREDICT_TRUE((expr))) {  \
166
      ABSL_INTERNAL_HARDENING_ABORT(); \
167
    }                                  \
168
  } while (false)
169
#else
170
#define ABSL_HARDENING_ASSERT(expr) ABSL_ASSERT(expr)
171
#endif
172
173
// ABSL_HARDENING_ASSERT_SLOW()
174
//
175
// `ABSL_HARDENING_ASSERT()` is like `ABSL_HARDENING_ASSERT()`,
176
//  but specifically for assertions whose predicates are too slow
177
//  to be enabled in many applications.
178
//
179
// When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT_SLOW()` is identical to
180
// `ABSL_ASSERT()`.
181
//
182
// See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on
183
// hardened mode.
184
#if ABSL_OPTION_HARDENED == 1 && defined(NDEBUG)
185
#define ABSL_HARDENING_ASSERT_SLOW(expr) ABSL_HARDENING_ASSERT(expr)
186
#else
187
#define ABSL_HARDENING_ASSERT_SLOW(expr) ABSL_ASSERT(expr)
188
#endif
189
190
#ifdef ABSL_HAVE_EXCEPTIONS
191
0
#define ABSL_INTERNAL_TRY try
192
#define ABSL_INTERNAL_CATCH_ANY catch (...)
193
0
#define ABSL_INTERNAL_RETHROW do { throw; } while (false)
194
#else  // ABSL_HAVE_EXCEPTIONS
195
#define ABSL_INTERNAL_TRY if (true)
196
#define ABSL_INTERNAL_CATCH_ANY else if (false)
197
#define ABSL_INTERNAL_RETHROW do {} while (false)
198
#endif  // ABSL_HAVE_EXCEPTIONS
199
200
// ABSL_REFACTOR_INLINE
201
//
202
// Marks a function or type for automated refactoring by go/cpp-inliner. It can
203
// be used on inline function definitions or type aliases in header files and
204
// should be combined with the `[[deprecated]]` attribute.
205
//
206
// Using `ABSL_REFACTOR_INLINE` differs from using the `[[deprecated]]` alone in
207
// the following ways:
208
//
209
// 1. New uses of the function or type will be discouraged via Tricorder
210
//    warnings.
211
// 2. If enabled via `METADATA`, automated changes will be sent out inlining the
212
//    functions's body or replacing the type where it is used.
213
//
214
// Examples:
215
//
216
// [[deprecated("Use NewFunc() instead")]] ABSL_REFACTOR_INLINE
217
// inline int OldFunc(int x) {
218
//   return NewFunc(x, 0);
219
// }
220
//
221
// using OldType [[deprecated("Use NewType instead")]] ABSL_REFACTOR_INLINE =
222
//     NewType;
223
//
224
// will mark `OldFunc` and `OldType` as deprecated, and the go/cpp-inliner
225
// service will replace calls to `OldFunc(x)` with calls to `NewFunc(x, 0)` and
226
// `OldType` with `NewType`. Once all replacements have been completed, the old
227
// function or type can be deleted.
228
//
229
// Internal note: Clang also allows `ABSL_REFACTOR_INLINE` to be used on
230
// using-declarations, but attributes on using-declarations are invalid in C++.
231
// (NOTE: This note refers to `using a::b ABSL_REFACTOR_INLINE;` and not
232
// `using b ABSL_REFACTOR_INLINE = a::b;`, which is OK.) Therefore:
233
//
234
// 1. In OSS: Do not use this on using-declarations. Such usage is invalid and
235
//    unsupported usage, and may break at any time.
236
// 2. In Google: Avoid such usage except as a last resort. Instead, prefer other
237
//    inlining approaches (such as type aliases or forwarding functions,
238
//    illustrated above) whenever possible. This is because Clang (currently)
239
//    does not honor the [[deprecated]] attribute on using-declarations, and
240
//    therefore cannot surface the deprecation to users in the middle of a
241
//    migration.
242
//
243
// See go/cpp-inliner for more information.
244
//
245
// Note: go/cpp-inliner is Google-internal service for automated refactoring.
246
// While open-source users do not have access to this service, the macro is
247
// provided for compatibility.
248
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::annotate) && !defined(__NVCC__)
249
#define ABSL_REFACTOR_INLINE                                                \
250
  _Pragma("clang diagnostic push") /* Avoid errors on using-declarations */ \
251
      _Pragma("clang diagnostic ignored \"-Wcxx-attribute-extension\"")     \
252
          [[clang::annotate("inline-me")]] _Pragma("clang diagnostic pop")
253
#else
254
#define ABSL_REFACTOR_INLINE
255
#endif
256
257
// ABSL_DEPRECATE_AND_INLINE()
258
//
259
// This is the original macro used by go/cpp-inliner that combines
260
// [[deprecated]] and ABSL_REFACTOR_INLINE.
261
//
262
// Examples:
263
//
264
// ABSL_DEPRECATE_AND_INLINE() inline int OldFunc(int x) {
265
//   return NewFunc(x, 0);
266
// }
267
//
268
// using OldType ABSL_DEPRECATE_AND_INLINE() = NewType;
269
//
270
// The combination of `[[deprecated("Use X instead")]]` and
271
// `ABSL_REFACTOR_INLINE` is preferred because it provides a more informative
272
// deprecation message to developers, especially those that do not have access
273
// to the automated refactoring capabilities of go/cpp-inliner.
274
#define ABSL_DEPRECATE_AND_INLINE() [[deprecated]] ABSL_REFACTOR_INLINE
275
276
// Requires the compiler to prove that the size of the given object is at least
277
// the expected amount.
278
#if ABSL_HAVE_ATTRIBUTE(diagnose_if) && ABSL_HAVE_BUILTIN(__builtin_object_size)
279
#define ABSL_INTERNAL_NEED_MIN_SIZE(Obj, N)                     \
280
  __attribute__((diagnose_if(__builtin_object_size(Obj, 0) < N, \
281
                             "object size provably too small "  \
282
                             "(this would corrupt memory)",     \
283
                             "error")))
284
#else
285
#define ABSL_INTERNAL_NEED_MIN_SIZE(Obj, N)
286
#endif
287
288
#endif  // ABSL_BASE_MACROS_H_