Coverage Report

Created: 2026-08-13 07:15

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
371k
  (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
// In C++11, `assert` can't be used portably within constexpr functions.
120
// `assert` also generates spurious unused-symbol warnings.
121
// ABSL_ASSERT functions as a runtime assert but works in C++11 constexpr
122
// functions, and maintains references to symbols.  Example:
123
//
124
// constexpr double Divide(double a, double b) {
125
//   return ABSL_ASSERT(b != 0), a / b;
126
// }
127
//
128
// This macro is inspired by
129
// https://akrzemi1.wordpress.com/2017/05/18/asserts-in-constexpr-functions/
130
#if defined(NDEBUG)
131
#define ABSL_ASSERT(expr) ABSL_INTERNAL_UNEVALUATED((expr) ? void() : void())
132
#else
133
#define ABSL_ASSERT(expr)                           \
134
1.11M
  (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
135
1.11M
                             : assert(false && #expr))  // NOLINT
136
#endif
137
138
// `ABSL_INTERNAL_HARDENING_ABORT()` controls how `ABSL_HARDENING_ASSERT()`
139
// aborts the program in release mode (when NDEBUG is defined). The
140
// implementation should abort the program as quickly as possible and ideally it
141
// should not be possible to ignore the abort request.
142
#if defined(__CUDACC__) || defined(__CUDA_ARCH__) || defined(__CUDA__)
143
#define ABSL_INTERNAL_HARDENING_ABORT()   \
144
  do {                                    \
145
    ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL(); \
146
    ABSL_INTERNAL_UNREACHABLE_IMPL();     \
147
  } while (false)
148
#else
149
#define ABSL_INTERNAL_HARDENING_ABORT() ::absl::base_internal::HardeningAbort()
150
#endif
151
152
// ABSL_HARDENING_ASSERT()
153
//
154
// `ABSL_HARDENING_ASSERT()` is like `ABSL_ASSERT()`, but used to implement
155
// runtime assertions that should be enabled in hardened builds even when
156
// `NDEBUG` is defined.
157
//
158
// When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT()` is identical to
159
// `ABSL_ASSERT()`.
160
//
161
// See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on
162
// hardened mode.
163
#if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG)
164
 #define ABSL_HARDENING_ASSERT(expr)    \
165
   do {                                 \
166
     if (!ABSL_PREDICT_TRUE((expr))) {  \
167
       ABSL_INTERNAL_HARDENING_ABORT(); \
168
     }                                  \
169
   } while (false)
170
#else
171
#define ABSL_HARDENING_ASSERT(expr) ABSL_ASSERT(expr)
172
#endif
173
174
// ABSL_HARDENING_ASSERT_SLOW()
175
//
176
// `ABSL_HARDENING_ASSERT()` is like `ABSL_HARDENING_ASSERT()`,
177
//  but specifically for assertions whose predicates are too slow
178
//  to be enabled in many applications.
179
//
180
// When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT_SLOW()` is identical to
181
// `ABSL_ASSERT()`.
182
//
183
// See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on
184
// hardened mode.
185
#if ABSL_OPTION_HARDENED == 1 && defined(NDEBUG)
186
#define ABSL_HARDENING_ASSERT_SLOW(expr) ABSL_HARDENING_ASSERT(expr)
187
#else
188
#define ABSL_HARDENING_ASSERT_SLOW(expr) ABSL_ASSERT(expr)
189
#endif
190
191
#ifdef ABSL_HAVE_EXCEPTIONS
192
0
#define ABSL_INTERNAL_TRY try
193
#define ABSL_INTERNAL_CATCH_ANY catch (...)
194
0
#define ABSL_INTERNAL_RETHROW do { throw; } while (false)
195
#else  // ABSL_HAVE_EXCEPTIONS
196
#define ABSL_INTERNAL_TRY if (true)
197
#define ABSL_INTERNAL_CATCH_ANY else if (false)
198
#define ABSL_INTERNAL_RETHROW do {} while (false)
199
#endif  // ABSL_HAVE_EXCEPTIONS
200
201
// ABSL_REFACTOR_INLINE
202
//
203
// Marks a function or type for automated refactoring by go/cpp-inliner. It can
204
// be used on inline function definitions or type aliases in header files and
205
// should be combined with the `[[deprecated]]` attribute.
206
//
207
// Using `ABSL_REFACTOR_INLINE` differs from using the `[[deprecated]]` alone in
208
// the following ways:
209
//
210
// 1. New uses of the function or type will be discouraged via Tricorder
211
//    warnings.
212
// 2. If enabled via `METADATA`, automated changes will be sent out inlining the
213
//    functions's body or replacing the type where it is used.
214
//
215
// Examples:
216
//
217
// [[deprecated("Use NewFunc() instead")]] ABSL_REFACTOR_INLINE
218
// inline int OldFunc(int x) {
219
//   return NewFunc(x, 0);
220
// }
221
//
222
// using OldType [[deprecated("Use NewType instead")]] ABSL_REFACTOR_INLINE =
223
//     NewType;
224
//
225
// will mark `OldFunc` and `OldType` as deprecated, and the go/cpp-inliner
226
// service will replace calls to `OldFunc(x)` with calls to `NewFunc(x, 0)` and
227
// `OldType` with `NewType`. Once all replacements have been completed, the old
228
// function or type can be deleted.
229
//
230
// Internal note: Clang also allows `ABSL_REFACTOR_INLINE` to be used on
231
// using-declarations, but attributes on using-declarations are invalid in C++.
232
// (NOTE: This note refers to `using a::b ABSL_REFACTOR_INLINE;` and not
233
// `using b ABSL_REFACTOR_INLINE = a::b;`, which is OK.) Therefore:
234
//
235
// 1. In OSS: Do not use this on using-declarations. Such usage is invalid and
236
//    unsupported usage, and may break at any time.
237
// 2. In Google: Avoid such usage except as a last resort. Instead, prefer other
238
//    inlining approaches (such as type aliases or forwarding functions,
239
//    illustrated above) whenever possible. This is because Clang (currently)
240
//    does not honor the [[deprecated]] attribute on using-declarations, and
241
//    therefore cannot surface the deprecation to users in the middle of a
242
//    migration.
243
//
244
// See go/cpp-inliner for more information.
245
//
246
// Note: go/cpp-inliner is Google-internal service for automated refactoring.
247
// While open-source users do not have access to this service, the macro is
248
// provided for compatibility.
249
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::annotate)
250
#define ABSL_REFACTOR_INLINE                                                \
251
  _Pragma("clang diagnostic push") /* Avoid errors on using-declarations */ \
252
      _Pragma("clang diagnostic ignored \"-Wcxx-attribute-extension\"")     \
253
          [[clang::annotate("inline-me")]] _Pragma("clang diagnostic pop")
254
#else
255
#define ABSL_REFACTOR_INLINE
256
#endif
257
258
// ABSL_DEPRECATE_AND_INLINE()
259
//
260
// This is the original macro used by go/cpp-inliner that combines
261
// [[deprecated]] and ABSL_REFACTOR_INLINE.
262
//
263
// Examples:
264
//
265
// ABSL_DEPRECATE_AND_INLINE() inline int OldFunc(int x) {
266
//   return NewFunc(x, 0);
267
// }
268
//
269
// using OldType ABSL_DEPRECATE_AND_INLINE() = NewType;
270
//
271
// The combination of `[[deprecated("Use X instead")]]` and
272
// `ABSL_REFACTOR_INLINE` is preferred because it provides a more informative
273
// deprecation message to developers, especially those that do not have access
274
// to the automated refactoring capabilities of go/cpp-inliner.
275
#define ABSL_DEPRECATE_AND_INLINE() [[deprecated]] ABSL_REFACTOR_INLINE
276
277
// Requires the compiler to prove that the size of the given object is at least
278
// the expected amount.
279
#if ABSL_HAVE_ATTRIBUTE(diagnose_if) && ABSL_HAVE_BUILTIN(__builtin_object_size)
280
#define ABSL_INTERNAL_NEED_MIN_SIZE(Obj, N)                     \
281
  __attribute__((diagnose_if(__builtin_object_size(Obj, 0) < N, \
282
                             "object size provably too small "  \
283
                             "(this would corrupt memory)",     \
284
                             "error")))
285
#else
286
#define ABSL_INTERNAL_NEED_MIN_SIZE(Obj, N)
287
#endif
288
289
#endif  // ABSL_BASE_MACROS_H_