2 // Copyright 2017 The Abseil Authors.
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
8 // https://www.apache.org/licenses/LICENSE-2.0
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.
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
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.
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.
28 #ifndef ABSL_BASE_MACROS_H_
29 #define ABSL_BASE_MACROS_H_
34 #include "absl/base/optimization.h"
35 #include "absl/base/port.h"
39 // Returns the number of elements in an array as a compile-time constant, which
40 // can be used in defining new arrays. If you use this macro on a pointer by
41 // mistake, you will get a compile-time error.
42 #define ABSL_ARRAYSIZE(array) \
43 (sizeof(::absl::macros_internal::ArraySizeHelper(array)))
46 namespace macros_internal {
47 // Note: this internal template function declaration is used by ABSL_ARRAYSIZE.
48 // The function doesn't need a definition, as we only use its type.
49 template <typename T, size_t N>
50 auto ArraySizeHelper(const T (&array)[N]) -> char (&)[N];
51 } // namespace macros_internal
56 // An enum used only as a constructor argument to indicate that a variable has
57 // static storage duration, and that the constructor should do nothing to its
58 // state. Use of this macro indicates to the reader that it is legal to
59 // declare a static instance of the class, provided the constructor is given
60 // the absl::base_internal::kLinkerInitialized argument.
62 // Normally, it is unsafe to declare a static variable that has a constructor or
63 // a destructor because invocation order is undefined. However, if the type can
64 // be zero-initialized (which the loader does for static variables) into a valid
65 // state and the type's destructor does not affect storage, then a constructor
66 // for static initialization can be declared.
70 // explicit MyClass(absl::base_internal:LinkerInitialized x) {}
73 // static MyClass my_global(absl::base_internal::kLinkerInitialized);
75 namespace base_internal {
76 enum LinkerInitialized {
77 kLinkerInitialized = 0,
79 } // namespace base_internal
82 // ABSL_FALLTHROUGH_INTENDED
84 // Annotates implicit fall-through between switch labels, allowing a case to
85 // indicate intentional fallthrough and turn off warnings about any lack of a
86 // `break` statement. The ABSL_FALLTHROUGH_INTENDED macro should be followed by
87 // a semicolon and can be used in most places where `break` can, provided that
88 // no statements exist between it and the next switch label.
95 // if (truth_is_out_there) {
97 // ABSL_FALLTHROUGH_INTENDED; // Use instead of/along with annotations
105 // Notes: when compiled with clang in C++11 mode, the ABSL_FALLTHROUGH_INTENDED
106 // macro is expanded to the [[clang::fallthrough]] attribute, which is analysed
107 // when performing switch labels fall-through diagnostic
108 // (`-Wimplicit-fallthrough`). See clang documentation on language extensions
110 // http://clang.llvm.org/docs/AttributeReference.html#fallthrough-clang-fallthrough
112 // When used with unsupported compilers, the ABSL_FALLTHROUGH_INTENDED macro
113 // has no effect on diagnostics. In any case this macro has no effect on runtime
114 // behavior and performance of code.
115 #ifdef ABSL_FALLTHROUGH_INTENDED
116 #error "ABSL_FALLTHROUGH_INTENDED should not be defined."
119 // TODO(zhangxy): Use c++17 standard [[fallthrough]] macro, when supported.
120 #if defined(__clang__) && defined(__has_warning)
121 #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
122 #define ABSL_FALLTHROUGH_INTENDED [[clang::fallthrough]]
124 #elif defined(__GNUC__) && __GNUC__ >= 7
125 #define ABSL_FALLTHROUGH_INTENDED [[gnu::fallthrough]]
128 #ifndef ABSL_FALLTHROUGH_INTENDED
129 #define ABSL_FALLTHROUGH_INTENDED \
136 // Marks a deprecated class, struct, enum, function, method and variable
137 // declarations. The macro argument is used as a custom diagnostic message (e.g.
138 // suggestion of a better alternative).
142 // class ABSL_DEPRECATED("Use Bar instead") Foo {...};
143 // ABSL_DEPRECATED("Use Baz instead") void Bar() {...}
145 // Every usage of a deprecated entity will trigger a warning when compiled with
146 // clang's `-Wdeprecated-declarations` option. This option is turned off by
147 // default, but the warnings will be reported by clang-tidy.
148 #if defined(__clang__) && __cplusplus >= 201103L
149 #define ABSL_DEPRECATED(message) __attribute__((deprecated(message)))
152 #ifndef ABSL_DEPRECATED
153 #define ABSL_DEPRECATED(message)
156 // ABSL_BAD_CALL_IF()
158 // Used on a function overload to trap bad calls: any call that matches the
159 // overload will cause a compile-time error. This macro uses a clang-specific
160 // "enable_if" attribute, as described at
161 // http://clang.llvm.org/docs/AttributeReference.html#enable-if
163 // Overloads which use this macro should be bracketed by
164 // `#ifdef ABSL_BAD_CALL_IF`.
168 // int isdigit(int c);
169 // #ifdef ABSL_BAD_CALL_IF
170 // int isdigit(int c)
171 // ABSL_BAD_CALL_IF(c <= -1 || c > 255,
172 // "'c' must have the value of an unsigned char or EOF");
173 // #endif // ABSL_BAD_CALL_IF
175 #if defined(__clang__)
176 # if __has_attribute(enable_if)
177 # define ABSL_BAD_CALL_IF(expr, msg) \
178 __attribute__((enable_if(expr, "Bad call trap"), unavailable(msg)))
184 // In C++11, `assert` can't be used portably within constexpr functions.
185 // ABSL_ASSERT functions as a runtime assert but works in C++11 constexpr
186 // functions. Example:
188 // constexpr double Divide(double a, double b) {
189 // return ABSL_ASSERT(b != 0), a / b;
192 // This macro is inspired by
193 // https://akrzemi1.wordpress.com/2017/05/18/asserts-in-constexpr-functions/
195 #define ABSL_ASSERT(expr) \
196 (false ? static_cast<void>(expr) : static_cast<void>(0))
198 #define ABSL_ASSERT(expr) \
199 (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
200 : [] { assert(false && #expr); }()) // NOLINT
203 #ifdef ABSL_HAVE_EXCEPTIONS
204 #define ABSL_INTERNAL_TRY try
205 #define ABSL_INTERNAL_CATCH_ANY catch (...)
206 #define ABSL_INTERNAL_RETHROW do { throw; } while (false)
207 #else // ABSL_HAVE_EXCEPTIONS
208 #define ABSL_INTERNAL_TRY if (true)
209 #define ABSL_INTERNAL_CATCH_ANY else if (false)
210 #define ABSL_INTERNAL_RETHROW do {} while (false)
211 #endif // ABSL_HAVE_EXCEPTIONS
213 #endif // ABSL_BASE_MACROS_H_