|
|
|||
File indexing completed on 2025-12-15 09:41:35
0001 // 0002 // Copyright 2017 The Abseil Authors. 0003 // 0004 // Licensed under the Apache License, Version 2.0 (the "License"); 0005 // you may not use this file except in compliance with the License. 0006 // You may obtain a copy of the License at 0007 // 0008 // https://www.apache.org/licenses/LICENSE-2.0 0009 // 0010 // Unless required by applicable law or agreed to in writing, software 0011 // distributed under the License is distributed on an "AS IS" BASIS, 0012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0013 // See the License for the specific language governing permissions and 0014 // limitations under the License. 0015 // 0016 // ----------------------------------------------------------------------------- 0017 // File: macros.h 0018 // ----------------------------------------------------------------------------- 0019 // 0020 // This header file defines the set of language macros used within Abseil code. 0021 // For the set of macros used to determine supported compilers and platforms, 0022 // see absl/base/config.h instead. 0023 // 0024 // This code is compiled directly on many platforms, including client 0025 // platforms like Windows, Mac, and embedded systems. Before making 0026 // any changes here, make sure that you're not breaking any platforms. 0027 0028 #ifndef ABSL_BASE_MACROS_H_ 0029 #define ABSL_BASE_MACROS_H_ 0030 0031 #include <cassert> 0032 #include <cstddef> 0033 0034 #include "absl/base/attributes.h" 0035 #include "absl/base/config.h" 0036 #include "absl/base/optimization.h" 0037 #include "absl/base/port.h" 0038 0039 // ABSL_ARRAYSIZE() 0040 // 0041 // Returns the number of elements in an array as a compile-time constant, which 0042 // can be used in defining new arrays. If you use this macro on a pointer by 0043 // mistake, you will get a compile-time error. 0044 #define ABSL_ARRAYSIZE(array) \ 0045 (sizeof(::absl::macros_internal::ArraySizeHelper(array))) 0046 0047 namespace absl { 0048 ABSL_NAMESPACE_BEGIN 0049 namespace macros_internal { 0050 // Note: this internal template function declaration is used by ABSL_ARRAYSIZE. 0051 // The function doesn't need a definition, as we only use its type. 0052 template <typename T, size_t N> 0053 auto ArraySizeHelper(const T (&array)[N]) -> char (&)[N]; 0054 } // namespace macros_internal 0055 ABSL_NAMESPACE_END 0056 } // namespace absl 0057 0058 // ABSL_BAD_CALL_IF() 0059 // 0060 // Used on a function overload to trap bad calls: any call that matches the 0061 // overload will cause a compile-time error. This macro uses a clang-specific 0062 // "enable_if" attribute, as described at 0063 // https://clang.llvm.org/docs/AttributeReference.html#enable-if 0064 // 0065 // Overloads which use this macro should be bracketed by 0066 // `#ifdef ABSL_BAD_CALL_IF`. 0067 // 0068 // Example: 0069 // 0070 // int isdigit(int c); 0071 // #ifdef ABSL_BAD_CALL_IF 0072 // int isdigit(int c) 0073 // ABSL_BAD_CALL_IF(c <= -1 || c > 255, 0074 // "'c' must have the value of an unsigned char or EOF"); 0075 // #endif // ABSL_BAD_CALL_IF 0076 #if ABSL_HAVE_ATTRIBUTE(enable_if) 0077 #define ABSL_BAD_CALL_IF(expr, msg) \ 0078 __attribute__((enable_if(expr, "Bad call trap"), unavailable(msg))) 0079 #endif 0080 0081 // ABSL_ASSERT() 0082 // 0083 // In C++11, `assert` can't be used portably within constexpr functions. 0084 // ABSL_ASSERT functions as a runtime assert but works in C++11 constexpr 0085 // functions. Example: 0086 // 0087 // constexpr double Divide(double a, double b) { 0088 // return ABSL_ASSERT(b != 0), a / b; 0089 // } 0090 // 0091 // This macro is inspired by 0092 // https://akrzemi1.wordpress.com/2017/05/18/asserts-in-constexpr-functions/ 0093 #if defined(NDEBUG) 0094 #define ABSL_ASSERT(expr) \ 0095 (false ? static_cast<void>(expr) : static_cast<void>(0)) 0096 #else 0097 #define ABSL_ASSERT(expr) \ 0098 (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \ 0099 : [] { assert(false && #expr); }()) // NOLINT 0100 #endif 0101 0102 // `ABSL_INTERNAL_HARDENING_ABORT()` controls how `ABSL_HARDENING_ASSERT()` 0103 // aborts the program in release mode (when NDEBUG is defined). The 0104 // implementation should abort the program as quickly as possible and ideally it 0105 // should not be possible to ignore the abort request. 0106 #define ABSL_INTERNAL_HARDENING_ABORT() \ 0107 do { \ 0108 ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL(); \ 0109 ABSL_INTERNAL_UNREACHABLE_IMPL(); \ 0110 } while (false) 0111 0112 // ABSL_HARDENING_ASSERT() 0113 // 0114 // `ABSL_HARDENING_ASSERT()` is like `ABSL_ASSERT()`, but used to implement 0115 // runtime assertions that should be enabled in hardened builds even when 0116 // `NDEBUG` is defined. 0117 // 0118 // When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT()` is identical to 0119 // `ABSL_ASSERT()`. 0120 // 0121 // See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on 0122 // hardened mode. 0123 #if ABSL_OPTION_HARDENED == 1 && defined(NDEBUG) 0124 #define ABSL_HARDENING_ASSERT(expr) \ 0125 (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \ 0126 : [] { ABSL_INTERNAL_HARDENING_ABORT(); }()) 0127 #else 0128 #define ABSL_HARDENING_ASSERT(expr) ABSL_ASSERT(expr) 0129 #endif 0130 0131 #ifdef ABSL_HAVE_EXCEPTIONS 0132 #define ABSL_INTERNAL_TRY try 0133 #define ABSL_INTERNAL_CATCH_ANY catch (...) 0134 #define ABSL_INTERNAL_RETHROW do { throw; } while (false) 0135 #else // ABSL_HAVE_EXCEPTIONS 0136 #define ABSL_INTERNAL_TRY if (true) 0137 #define ABSL_INTERNAL_CATCH_ANY else if (false) 0138 #define ABSL_INTERNAL_RETHROW do {} while (false) 0139 #endif // ABSL_HAVE_EXCEPTIONS 0140 0141 // ABSL_DEPRECATE_AND_INLINE() 0142 // 0143 // Marks a function or type alias as deprecated and tags it to be picked up for 0144 // automated refactoring by go/cpp-inliner. It can added to inline function 0145 // definitions or type aliases. It should only be used within a header file. It 0146 // differs from `ABSL_DEPRECATED` in the following ways: 0147 // 0148 // 1. New uses of the function or type will be discouraged via Tricorder 0149 // warnings. 0150 // 2. If enabled via `METADATA`, automated changes will be sent out inlining the 0151 // functions's body or replacing the type where it is used. 0152 // 0153 // For example: 0154 // 0155 // ABSL_DEPRECATE_AND_INLINE() inline int OldFunc(int x) { 0156 // return NewFunc(x, 0); 0157 // } 0158 // 0159 // will mark `OldFunc` as deprecated, and the go/cpp-inliner service will 0160 // replace calls to `OldFunc(x)` with calls to `NewFunc(x, 0)`. Once all calls 0161 // to `OldFunc` have been replaced, `OldFunc` can be deleted. 0162 // 0163 // See go/cpp-inliner for more information. 0164 // 0165 // Note: go/cpp-inliner is Google-internal service for automated refactoring. 0166 // While open-source users do not have access to this service, the macro is 0167 // provided for compatibility, and so that users receive deprecation warnings. 0168 #if ABSL_HAVE_CPP_ATTRIBUTE(deprecated) && \ 0169 ABSL_HAVE_CPP_ATTRIBUTE(clang::annotate) 0170 #define ABSL_DEPRECATE_AND_INLINE() [[deprecated, clang::annotate("inline-me")]] 0171 #elif ABSL_HAVE_CPP_ATTRIBUTE(deprecated) 0172 #define ABSL_DEPRECATE_AND_INLINE() [[deprecated]] 0173 #else 0174 #define ABSL_DEPRECATE_AND_INLINE() 0175 #endif 0176 0177 // Requires the compiler to prove that the size of the given object is at least 0178 // the expected amount. 0179 #if ABSL_HAVE_ATTRIBUTE(diagnose_if) && ABSL_HAVE_BUILTIN(__builtin_object_size) 0180 #define ABSL_INTERNAL_NEED_MIN_SIZE(Obj, N) \ 0181 __attribute__((diagnose_if(__builtin_object_size(Obj, 0) < N, \ 0182 "object size provably too small " \ 0183 "(this would corrupt memory)", \ 0184 "error"))) 0185 #else 0186 #define ABSL_INTERNAL_NEED_MIN_SIZE(Obj, N) 0187 #endif 0188 0189 #endif // ABSL_BASE_MACROS_H_
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|