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