Coverage Report

Created: 2026-07-16 06:43

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