misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 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 | // |
nik7273 | 38b7043 | 2019-03-08 10:27:53 -0500 | [diff] [blame] | 8 | // https://www.apache.org/licenses/LICENSE-2.0 |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 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. |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 27 | |
| 28 | #ifndef ABSL_BASE_MACROS_H_ |
| 29 | #define ABSL_BASE_MACROS_H_ |
| 30 | |
Abseil Team | dedb4ee | 2017-10-24 10:37:49 -0700 | [diff] [blame] | 31 | #include <cassert> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 32 | #include <cstddef> |
| 33 | |
Abseil Team | a4b757b | 2019-12-02 10:41:53 -0800 | [diff] [blame] | 34 | #include "absl/base/attributes.h" |
Abseil Team | 518f175 | 2020-03-23 13:16:18 -0700 | [diff] [blame] | 35 | #include "absl/base/config.h" |
Abseil Team | 8a394b1 | 2019-05-17 10:51:37 -0700 | [diff] [blame] | 36 | #include "absl/base/optimization.h" |
Abseil Team | 1031858 | 2024-08-29 10:05:42 -0700 | [diff] [blame] | 37 | #include "absl/base/options.h" |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 38 | #include "absl/base/port.h" |
| 39 | |
| 40 | // ABSL_ARRAYSIZE() |
| 41 | // |
Abseil Team | cd95e71 | 2018-05-09 13:57:56 -0700 | [diff] [blame] | 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 |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 44 | // mistake, you will get a compile-time error. |
Abseil Team | cd95e71 | 2018-05-09 13:57:56 -0700 | [diff] [blame] | 45 | #define ABSL_ARRAYSIZE(array) \ |
| 46 | (sizeof(::absl::macros_internal::ArraySizeHelper(array))) |
| 47 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 48 | namespace absl { |
Abseil Team | 1e39f86 | 2019-12-10 09:03:32 -0800 | [diff] [blame] | 49 | ABSL_NAMESPACE_BEGIN |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 50 | namespace macros_internal { |
Abseil Team | cd95e71 | 2018-05-09 13:57:56 -0700 | [diff] [blame] | 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. |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 53 | template <typename T, size_t N> |
Abseil Team | 7fda099 | 2018-02-27 13:38:47 -0800 | [diff] [blame] | 54 | auto ArraySizeHelper(const T (&array)[N]) -> char (&)[N]; |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 55 | } // namespace macros_internal |
Abseil Team | 1e39f86 | 2019-12-10 09:03:32 -0800 | [diff] [blame] | 56 | ABSL_NAMESPACE_END |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 57 | } // namespace absl |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 58 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 59 | // ABSL_BAD_CALL_IF() |
| 60 | // |
| 61 | // Used on a function overload to trap bad calls: any call that matches the |
| 62 | // overload will cause a compile-time error. This macro uses a clang-specific |
| 63 | // "enable_if" attribute, as described at |
nlewycky | a2e6ade | 2020-01-13 11:12:06 -0800 | [diff] [blame] | 64 | // https://clang.llvm.org/docs/AttributeReference.html#enable-if |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 65 | // |
| 66 | // Overloads which use this macro should be bracketed by |
| 67 | // `#ifdef ABSL_BAD_CALL_IF`. |
| 68 | // |
| 69 | // Example: |
| 70 | // |
| 71 | // int isdigit(int c); |
| 72 | // #ifdef ABSL_BAD_CALL_IF |
| 73 | // int isdigit(int c) |
| 74 | // ABSL_BAD_CALL_IF(c <= -1 || c > 255, |
| 75 | // "'c' must have the value of an unsigned char or EOF"); |
| 76 | // #endif // ABSL_BAD_CALL_IF |
Abseil Team | a4b757b | 2019-12-02 10:41:53 -0800 | [diff] [blame] | 77 | #if ABSL_HAVE_ATTRIBUTE(enable_if) |
| 78 | #define ABSL_BAD_CALL_IF(expr, msg) \ |
| 79 | __attribute__((enable_if(expr, "Bad call trap"), unavailable(msg))) |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 80 | #endif |
| 81 | |
| 82 | // ABSL_ASSERT() |
| 83 | // |
| 84 | // In C++11, `assert` can't be used portably within constexpr functions. |
| 85 | // ABSL_ASSERT functions as a runtime assert but works in C++11 constexpr |
| 86 | // functions. Example: |
| 87 | // |
| 88 | // constexpr double Divide(double a, double b) { |
| 89 | // return ABSL_ASSERT(b != 0), a / b; |
| 90 | // } |
| 91 | // |
| 92 | // This macro is inspired by |
| 93 | // https://akrzemi1.wordpress.com/2017/05/18/asserts-in-constexpr-functions/ |
| 94 | #if defined(NDEBUG) |
Abseil Team | 666fc12 | 2019-04-04 08:13:57 -0700 | [diff] [blame] | 95 | #define ABSL_ASSERT(expr) \ |
| 96 | (false ? static_cast<void>(expr) : static_cast<void>(0)) |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 97 | #else |
Abseil Team | 666fc12 | 2019-04-04 08:13:57 -0700 | [diff] [blame] | 98 | #define ABSL_ASSERT(expr) \ |
| 99 | (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \ |
Abseil Team | eb686c0 | 2018-06-13 11:44:15 -0700 | [diff] [blame] | 100 | : [] { assert(false && #expr); }()) // NOLINT |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 101 | #endif |
| 102 | |
Abseil Team | 518f175 | 2020-03-23 13:16:18 -0700 | [diff] [blame] | 103 | // `ABSL_INTERNAL_HARDENING_ABORT()` controls how `ABSL_HARDENING_ASSERT()` |
| 104 | // aborts the program in release mode (when NDEBUG is defined). The |
| 105 | // implementation should abort the program as quickly as possible and ideally it |
| 106 | // should not be possible to ignore the abort request. |
Derek Mauro | 6a87605 | 2022-12-21 07:31:39 -0800 | [diff] [blame] | 107 | #define ABSL_INTERNAL_HARDENING_ABORT() \ |
| 108 | do { \ |
| 109 | ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL(); \ |
| 110 | ABSL_INTERNAL_UNREACHABLE_IMPL(); \ |
Abseil Team | 518f175 | 2020-03-23 13:16:18 -0700 | [diff] [blame] | 111 | } while (false) |
Abseil Team | 518f175 | 2020-03-23 13:16:18 -0700 | [diff] [blame] | 112 | |
| 113 | // ABSL_HARDENING_ASSERT() |
| 114 | // |
Abseil Team | 79e0dc1 | 2020-03-26 08:48:01 -0700 | [diff] [blame] | 115 | // `ABSL_HARDENING_ASSERT()` is like `ABSL_ASSERT()`, but used to implement |
Abseil Team | 518f175 | 2020-03-23 13:16:18 -0700 | [diff] [blame] | 116 | // runtime assertions that should be enabled in hardened builds even when |
| 117 | // `NDEBUG` is defined. |
| 118 | // |
Abseil Team | 79e0dc1 | 2020-03-26 08:48:01 -0700 | [diff] [blame] | 119 | // When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT()` is identical to |
Abseil Team | 518f175 | 2020-03-23 13:16:18 -0700 | [diff] [blame] | 120 | // `ABSL_ASSERT()`. |
| 121 | // |
| 122 | // See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on |
| 123 | // hardened mode. |
Abseil Team | 1031858 | 2024-08-29 10:05:42 -0700 | [diff] [blame] | 124 | #if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG) |
Abseil Team | 518f175 | 2020-03-23 13:16:18 -0700 | [diff] [blame] | 125 | #define ABSL_HARDENING_ASSERT(expr) \ |
| 126 | (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \ |
| 127 | : [] { ABSL_INTERNAL_HARDENING_ABORT(); }()) |
| 128 | #else |
| 129 | #define ABSL_HARDENING_ASSERT(expr) ABSL_ASSERT(expr) |
| 130 | #endif |
| 131 | |
Abseil Team | 1031858 | 2024-08-29 10:05:42 -0700 | [diff] [blame] | 132 | // ABSL_HARDENING_ASSERT_SLOW() |
| 133 | // |
| 134 | // `ABSL_HARDENING_ASSERT()` is like `ABSL_HARDENING_ASSERT()`, |
| 135 | // but specifically for assertions whose predicates are too slow |
| 136 | // to be enabled in many applications. |
| 137 | // |
| 138 | // When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT_SLOW()` is identical to |
| 139 | // `ABSL_ASSERT()`. |
| 140 | // |
| 141 | // See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on |
| 142 | // hardened mode. |
| 143 | #if ABSL_OPTION_HARDENED == 1 && defined(NDEBUG) |
| 144 | #define ABSL_HARDENING_ASSERT_SLOW(expr) \ |
| 145 | (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \ |
| 146 | : [] { ABSL_INTERNAL_HARDENING_ABORT(); }()) |
| 147 | #else |
| 148 | #define ABSL_HARDENING_ASSERT_SLOW(expr) ABSL_ASSERT(expr) |
| 149 | #endif |
| 150 | |
Abseil Team | a4c3fff | 2018-10-29 15:53:34 -0700 | [diff] [blame] | 151 | #ifdef ABSL_HAVE_EXCEPTIONS |
| 152 | #define ABSL_INTERNAL_TRY try |
| 153 | #define ABSL_INTERNAL_CATCH_ANY catch (...) |
| 154 | #define ABSL_INTERNAL_RETHROW do { throw; } while (false) |
| 155 | #else // ABSL_HAVE_EXCEPTIONS |
| 156 | #define ABSL_INTERNAL_TRY if (true) |
| 157 | #define ABSL_INTERNAL_CATCH_ANY else if (false) |
| 158 | #define ABSL_INTERNAL_RETHROW do {} while (false) |
| 159 | #endif // ABSL_HAVE_EXCEPTIONS |
| 160 | |
Derek Mauro | 36442dd | 2024-02-08 08:16:36 -0800 | [diff] [blame] | 161 | // ABSL_DEPRECATE_AND_INLINE() |
| 162 | // |
| 163 | // Marks a function or type alias as deprecated and tags it to be picked up for |
| 164 | // automated refactoring by go/cpp-inliner. It can added to inline function |
| 165 | // definitions or type aliases. It should only be used within a header file. It |
| 166 | // differs from `ABSL_DEPRECATED` in the following ways: |
| 167 | // |
| 168 | // 1. New uses of the function or type will be discouraged via Tricorder |
| 169 | // warnings. |
| 170 | // 2. If enabled via `METADATA`, automated changes will be sent out inlining the |
| 171 | // functions's body or replacing the type where it is used. |
| 172 | // |
| 173 | // For example: |
| 174 | // |
| 175 | // ABSL_DEPRECATE_AND_INLINE() inline int OldFunc(int x) { |
| 176 | // return NewFunc(x, 0); |
| 177 | // } |
| 178 | // |
| 179 | // will mark `OldFunc` as deprecated, and the go/cpp-inliner service will |
| 180 | // replace calls to `OldFunc(x)` with calls to `NewFunc(x, 0)`. Once all calls |
| 181 | // to `OldFunc` have been replaced, `OldFunc` can be deleted. |
| 182 | // |
| 183 | // See go/cpp-inliner for more information. |
| 184 | // |
| 185 | // Note: go/cpp-inliner is Google-internal service for automated refactoring. |
| 186 | // While open-source users do not have access to this service, the macro is |
| 187 | // provided for compatibility, and so that users receive deprecation warnings. |
| 188 | #if ABSL_HAVE_CPP_ATTRIBUTE(deprecated) && \ |
| 189 | ABSL_HAVE_CPP_ATTRIBUTE(clang::annotate) |
| 190 | #define ABSL_DEPRECATE_AND_INLINE() [[deprecated, clang::annotate("inline-me")]] |
| 191 | #elif ABSL_HAVE_CPP_ATTRIBUTE(deprecated) |
| 192 | #define ABSL_DEPRECATE_AND_INLINE() [[deprecated]] |
| 193 | #else |
| 194 | #define ABSL_DEPRECATE_AND_INLINE() |
| 195 | #endif |
| 196 | |
Abseil Team | e7858c7 | 2024-03-26 11:41:28 -0700 | [diff] [blame] | 197 | // Requires the compiler to prove that the size of the given object is at least |
| 198 | // the expected amount. |
| 199 | #if ABSL_HAVE_ATTRIBUTE(diagnose_if) && ABSL_HAVE_BUILTIN(__builtin_object_size) |
| 200 | #define ABSL_INTERNAL_NEED_MIN_SIZE(Obj, N) \ |
| 201 | __attribute__((diagnose_if(__builtin_object_size(Obj, 0) < N, \ |
| 202 | "object size provably too small " \ |
| 203 | "(this would corrupt memory)", \ |
| 204 | "error"))) |
| 205 | #else |
| 206 | #define ABSL_INTERNAL_NEED_MIN_SIZE(Obj, N) |
| 207 | #endif |
| 208 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 209 | #endif // ABSL_BASE_MACROS_H_ |