xref: /aosp_15_r20/external/abseil-cpp/absl/base/macros.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker //
2*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors.
3*9356374aSAndroid Build Coastguard Worker //
4*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*9356374aSAndroid Build Coastguard Worker //
8*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
9*9356374aSAndroid Build Coastguard Worker //
10*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*9356374aSAndroid Build Coastguard Worker // limitations under the License.
15*9356374aSAndroid Build Coastguard Worker //
16*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
17*9356374aSAndroid Build Coastguard Worker // File: macros.h
18*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
19*9356374aSAndroid Build Coastguard Worker //
20*9356374aSAndroid Build Coastguard Worker // This header file defines the set of language macros used within Abseil code.
21*9356374aSAndroid Build Coastguard Worker // For the set of macros used to determine supported compilers and platforms,
22*9356374aSAndroid Build Coastguard Worker // see absl/base/config.h instead.
23*9356374aSAndroid Build Coastguard Worker //
24*9356374aSAndroid Build Coastguard Worker // This code is compiled directly on many platforms, including client
25*9356374aSAndroid Build Coastguard Worker // platforms like Windows, Mac, and embedded systems.  Before making
26*9356374aSAndroid Build Coastguard Worker // any changes here, make sure that you're not breaking any platforms.
27*9356374aSAndroid Build Coastguard Worker 
28*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_BASE_MACROS_H_
29*9356374aSAndroid Build Coastguard Worker #define ABSL_BASE_MACROS_H_
30*9356374aSAndroid Build Coastguard Worker 
31*9356374aSAndroid Build Coastguard Worker #include <cassert>
32*9356374aSAndroid Build Coastguard Worker #include <cstddef>
33*9356374aSAndroid Build Coastguard Worker 
34*9356374aSAndroid Build Coastguard Worker #include "absl/base/attributes.h"
35*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
36*9356374aSAndroid Build Coastguard Worker #include "absl/base/optimization.h"
37*9356374aSAndroid Build Coastguard Worker #include "absl/base/port.h"
38*9356374aSAndroid Build Coastguard Worker 
39*9356374aSAndroid Build Coastguard Worker // ABSL_ARRAYSIZE()
40*9356374aSAndroid Build Coastguard Worker //
41*9356374aSAndroid Build Coastguard Worker // Returns the number of elements in an array as a compile-time constant, which
42*9356374aSAndroid Build Coastguard Worker // can be used in defining new arrays. If you use this macro on a pointer by
43*9356374aSAndroid Build Coastguard Worker // mistake, you will get a compile-time error.
44*9356374aSAndroid Build Coastguard Worker #define ABSL_ARRAYSIZE(array) \
45*9356374aSAndroid Build Coastguard Worker   (sizeof(::absl::macros_internal::ArraySizeHelper(array)))
46*9356374aSAndroid Build Coastguard Worker 
47*9356374aSAndroid Build Coastguard Worker namespace absl {
48*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
49*9356374aSAndroid Build Coastguard Worker namespace macros_internal {
50*9356374aSAndroid Build Coastguard Worker // Note: this internal template function declaration is used by ABSL_ARRAYSIZE.
51*9356374aSAndroid Build Coastguard Worker // The function doesn't need a definition, as we only use its type.
52*9356374aSAndroid Build Coastguard Worker template <typename T, size_t N>
53*9356374aSAndroid Build Coastguard Worker auto ArraySizeHelper(const T (&array)[N]) -> char (&)[N];
54*9356374aSAndroid Build Coastguard Worker }  // namespace macros_internal
55*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
56*9356374aSAndroid Build Coastguard Worker }  // namespace absl
57*9356374aSAndroid Build Coastguard Worker 
58*9356374aSAndroid Build Coastguard Worker // ABSL_BAD_CALL_IF()
59*9356374aSAndroid Build Coastguard Worker //
60*9356374aSAndroid Build Coastguard Worker // Used on a function overload to trap bad calls: any call that matches the
61*9356374aSAndroid Build Coastguard Worker // overload will cause a compile-time error. This macro uses a clang-specific
62*9356374aSAndroid Build Coastguard Worker // "enable_if" attribute, as described at
63*9356374aSAndroid Build Coastguard Worker // https://clang.llvm.org/docs/AttributeReference.html#enable-if
64*9356374aSAndroid Build Coastguard Worker //
65*9356374aSAndroid Build Coastguard Worker // Overloads which use this macro should be bracketed by
66*9356374aSAndroid Build Coastguard Worker // `#ifdef ABSL_BAD_CALL_IF`.
67*9356374aSAndroid Build Coastguard Worker //
68*9356374aSAndroid Build Coastguard Worker // Example:
69*9356374aSAndroid Build Coastguard Worker //
70*9356374aSAndroid Build Coastguard Worker //   int isdigit(int c);
71*9356374aSAndroid Build Coastguard Worker //   #ifdef ABSL_BAD_CALL_IF
72*9356374aSAndroid Build Coastguard Worker //   int isdigit(int c)
73*9356374aSAndroid Build Coastguard Worker //     ABSL_BAD_CALL_IF(c <= -1 || c > 255,
74*9356374aSAndroid Build Coastguard Worker //                       "'c' must have the value of an unsigned char or EOF");
75*9356374aSAndroid Build Coastguard Worker //   #endif // ABSL_BAD_CALL_IF
76*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(enable_if)
77*9356374aSAndroid Build Coastguard Worker #define ABSL_BAD_CALL_IF(expr, msg) \
78*9356374aSAndroid Build Coastguard Worker   __attribute__((enable_if(expr, "Bad call trap"), unavailable(msg)))
79*9356374aSAndroid Build Coastguard Worker #endif
80*9356374aSAndroid Build Coastguard Worker 
81*9356374aSAndroid Build Coastguard Worker // ABSL_ASSERT()
82*9356374aSAndroid Build Coastguard Worker //
83*9356374aSAndroid Build Coastguard Worker // In C++11, `assert` can't be used portably within constexpr functions.
84*9356374aSAndroid Build Coastguard Worker // ABSL_ASSERT functions as a runtime assert but works in C++11 constexpr
85*9356374aSAndroid Build Coastguard Worker // functions.  Example:
86*9356374aSAndroid Build Coastguard Worker //
87*9356374aSAndroid Build Coastguard Worker // constexpr double Divide(double a, double b) {
88*9356374aSAndroid Build Coastguard Worker //   return ABSL_ASSERT(b != 0), a / b;
89*9356374aSAndroid Build Coastguard Worker // }
90*9356374aSAndroid Build Coastguard Worker //
91*9356374aSAndroid Build Coastguard Worker // This macro is inspired by
92*9356374aSAndroid Build Coastguard Worker // https://akrzemi1.wordpress.com/2017/05/18/asserts-in-constexpr-functions/
93*9356374aSAndroid Build Coastguard Worker #if defined(NDEBUG)
94*9356374aSAndroid Build Coastguard Worker #define ABSL_ASSERT(expr) \
95*9356374aSAndroid Build Coastguard Worker   (false ? static_cast<void>(expr) : static_cast<void>(0))
96*9356374aSAndroid Build Coastguard Worker #else
97*9356374aSAndroid Build Coastguard Worker #define ABSL_ASSERT(expr)                           \
98*9356374aSAndroid Build Coastguard Worker   (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
99*9356374aSAndroid Build Coastguard Worker                              : [] { assert(false && #expr); }())  // NOLINT
100*9356374aSAndroid Build Coastguard Worker #endif
101*9356374aSAndroid Build Coastguard Worker 
102*9356374aSAndroid Build Coastguard Worker // `ABSL_INTERNAL_HARDENING_ABORT()` controls how `ABSL_HARDENING_ASSERT()`
103*9356374aSAndroid Build Coastguard Worker // aborts the program in release mode (when NDEBUG is defined). The
104*9356374aSAndroid Build Coastguard Worker // implementation should abort the program as quickly as possible and ideally it
105*9356374aSAndroid Build Coastguard Worker // should not be possible to ignore the abort request.
106*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_HARDENING_ABORT()   \
107*9356374aSAndroid Build Coastguard Worker   do {                                    \
108*9356374aSAndroid Build Coastguard Worker     ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL(); \
109*9356374aSAndroid Build Coastguard Worker     ABSL_INTERNAL_UNREACHABLE_IMPL();     \
110*9356374aSAndroid Build Coastguard Worker   } while (false)
111*9356374aSAndroid Build Coastguard Worker 
112*9356374aSAndroid Build Coastguard Worker // ABSL_HARDENING_ASSERT()
113*9356374aSAndroid Build Coastguard Worker //
114*9356374aSAndroid Build Coastguard Worker // `ABSL_HARDENING_ASSERT()` is like `ABSL_ASSERT()`, but used to implement
115*9356374aSAndroid Build Coastguard Worker // runtime assertions that should be enabled in hardened builds even when
116*9356374aSAndroid Build Coastguard Worker // `NDEBUG` is defined.
117*9356374aSAndroid Build Coastguard Worker //
118*9356374aSAndroid Build Coastguard Worker // When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT()` is identical to
119*9356374aSAndroid Build Coastguard Worker // `ABSL_ASSERT()`.
120*9356374aSAndroid Build Coastguard Worker //
121*9356374aSAndroid Build Coastguard Worker // See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on
122*9356374aSAndroid Build Coastguard Worker // hardened mode.
123*9356374aSAndroid Build Coastguard Worker #if ABSL_OPTION_HARDENED == 1 && defined(NDEBUG)
124*9356374aSAndroid Build Coastguard Worker #define ABSL_HARDENING_ASSERT(expr)                 \
125*9356374aSAndroid Build Coastguard Worker   (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
126*9356374aSAndroid Build Coastguard Worker                              : [] { ABSL_INTERNAL_HARDENING_ABORT(); }())
127*9356374aSAndroid Build Coastguard Worker #else
128*9356374aSAndroid Build Coastguard Worker #define ABSL_HARDENING_ASSERT(expr) ABSL_ASSERT(expr)
129*9356374aSAndroid Build Coastguard Worker #endif
130*9356374aSAndroid Build Coastguard Worker 
131*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
132*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_TRY try
133*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_CATCH_ANY catch (...)
134*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_RETHROW do { throw; } while (false)
135*9356374aSAndroid Build Coastguard Worker #else  // ABSL_HAVE_EXCEPTIONS
136*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_TRY if (true)
137*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_CATCH_ANY else if (false)
138*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_RETHROW do {} while (false)
139*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_HAVE_EXCEPTIONS
140*9356374aSAndroid Build Coastguard Worker 
141*9356374aSAndroid Build Coastguard Worker // ABSL_DEPRECATE_AND_INLINE()
142*9356374aSAndroid Build Coastguard Worker //
143*9356374aSAndroid Build Coastguard Worker // Marks a function or type alias as deprecated and tags it to be picked up for
144*9356374aSAndroid Build Coastguard Worker // automated refactoring by go/cpp-inliner. It can added to inline function
145*9356374aSAndroid Build Coastguard Worker // definitions or type aliases. It should only be used within a header file. It
146*9356374aSAndroid Build Coastguard Worker // differs from `ABSL_DEPRECATED` in the following ways:
147*9356374aSAndroid Build Coastguard Worker //
148*9356374aSAndroid Build Coastguard Worker // 1. New uses of the function or type will be discouraged via Tricorder
149*9356374aSAndroid Build Coastguard Worker //    warnings.
150*9356374aSAndroid Build Coastguard Worker // 2. If enabled via `METADATA`, automated changes will be sent out inlining the
151*9356374aSAndroid Build Coastguard Worker //    functions's body or replacing the type where it is used.
152*9356374aSAndroid Build Coastguard Worker //
153*9356374aSAndroid Build Coastguard Worker // For example:
154*9356374aSAndroid Build Coastguard Worker //
155*9356374aSAndroid Build Coastguard Worker // ABSL_DEPRECATE_AND_INLINE() inline int OldFunc(int x) {
156*9356374aSAndroid Build Coastguard Worker //   return NewFunc(x, 0);
157*9356374aSAndroid Build Coastguard Worker // }
158*9356374aSAndroid Build Coastguard Worker //
159*9356374aSAndroid Build Coastguard Worker // will mark `OldFunc` as deprecated, and the go/cpp-inliner service will
160*9356374aSAndroid Build Coastguard Worker // replace calls to `OldFunc(x)` with calls to `NewFunc(x, 0)`. Once all calls
161*9356374aSAndroid Build Coastguard Worker // to `OldFunc` have been replaced, `OldFunc` can be deleted.
162*9356374aSAndroid Build Coastguard Worker //
163*9356374aSAndroid Build Coastguard Worker // See go/cpp-inliner for more information.
164*9356374aSAndroid Build Coastguard Worker //
165*9356374aSAndroid Build Coastguard Worker // Note: go/cpp-inliner is Google-internal service for automated refactoring.
166*9356374aSAndroid Build Coastguard Worker // While open-source users do not have access to this service, the macro is
167*9356374aSAndroid Build Coastguard Worker // provided for compatibility, and so that users receive deprecation warnings.
168*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_CPP_ATTRIBUTE(deprecated) && \
169*9356374aSAndroid Build Coastguard Worker     ABSL_HAVE_CPP_ATTRIBUTE(clang::annotate)
170*9356374aSAndroid Build Coastguard Worker #define ABSL_DEPRECATE_AND_INLINE() [[deprecated, clang::annotate("inline-me")]]
171*9356374aSAndroid Build Coastguard Worker #elif ABSL_HAVE_CPP_ATTRIBUTE(deprecated)
172*9356374aSAndroid Build Coastguard Worker #define ABSL_DEPRECATE_AND_INLINE() [[deprecated]]
173*9356374aSAndroid Build Coastguard Worker #else
174*9356374aSAndroid Build Coastguard Worker #define ABSL_DEPRECATE_AND_INLINE()
175*9356374aSAndroid Build Coastguard Worker #endif
176*9356374aSAndroid Build Coastguard Worker 
177*9356374aSAndroid Build Coastguard Worker // Requires the compiler to prove that the size of the given object is at least
178*9356374aSAndroid Build Coastguard Worker // the expected amount.
179*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_ATTRIBUTE(diagnose_if) && ABSL_HAVE_BUILTIN(__builtin_object_size)
180*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_NEED_MIN_SIZE(Obj, N)                     \
181*9356374aSAndroid Build Coastguard Worker   __attribute__((diagnose_if(__builtin_object_size(Obj, 0) < N, \
182*9356374aSAndroid Build Coastguard Worker                              "object size provably too small "  \
183*9356374aSAndroid Build Coastguard Worker                              "(this would corrupt memory)",     \
184*9356374aSAndroid Build Coastguard Worker                              "error")))
185*9356374aSAndroid Build Coastguard Worker #else
186*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_NEED_MIN_SIZE(Obj, N)
187*9356374aSAndroid Build Coastguard Worker #endif
188*9356374aSAndroid Build Coastguard Worker 
189*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_BASE_MACROS_H_
190