macros.h
Go to the documentation of this file.
00001 //
00002 // Copyright 2017 The Abseil Authors.
00003 //
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //
00008 //      https://www.apache.org/licenses/LICENSE-2.0
00009 //
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 //
00016 // -----------------------------------------------------------------------------
00017 // File: macros.h
00018 // -----------------------------------------------------------------------------
00019 //
00020 // This header file defines the set of language macros used within Abseil code.
00021 // For the set of macros used to determine supported compilers and platforms,
00022 // see absl/base/config.h instead.
00023 //
00024 // This code is compiled directly on many platforms, including client
00025 // platforms like Windows, Mac, and embedded systems.  Before making
00026 // any changes here, make sure that you're not breaking any platforms.
00027 
00028 #ifndef ABSL_BASE_MACROS_H_
00029 #define ABSL_BASE_MACROS_H_
00030 
00031 #include <cassert>
00032 #include <cstddef>
00033 
00034 #include "absl/base/optimization.h"
00035 #include "absl/base/port.h"
00036 
00037 // ABSL_ARRAYSIZE()
00038 //
00039 // Returns the number of elements in an array as a compile-time constant, which
00040 // can be used in defining new arrays. If you use this macro on a pointer by
00041 // mistake, you will get a compile-time error.
00042 #define ABSL_ARRAYSIZE(array) \
00043   (sizeof(::absl::macros_internal::ArraySizeHelper(array)))
00044 
00045 namespace absl {
00046 namespace macros_internal {
00047 // Note: this internal template function declaration is used by ABSL_ARRAYSIZE.
00048 // The function doesn't need a definition, as we only use its type.
00049 template <typename T, size_t N>
00050 auto ArraySizeHelper(const T (&array)[N]) -> char (&)[N];
00051 }  // namespace macros_internal
00052 }  // namespace absl
00053 
00054 // kLinkerInitialized
00055 //
00056 // An enum used only as a constructor argument to indicate that a variable has
00057 // static storage duration, and that the constructor should do nothing to its
00058 // state. Use of this macro indicates to the reader that it is legal to
00059 // declare a static instance of the class, provided the constructor is given
00060 // the absl::base_internal::kLinkerInitialized argument.
00061 //
00062 // Normally, it is unsafe to declare a static variable that has a constructor or
00063 // a destructor because invocation order is undefined. However, if the type can
00064 // be zero-initialized (which the loader does for static variables) into a valid
00065 // state and the type's destructor does not affect storage, then a constructor
00066 // for static initialization can be declared.
00067 //
00068 // Example:
00069 //       // Declaration
00070 //       explicit MyClass(absl::base_internal:LinkerInitialized x) {}
00071 //
00072 //       // Invocation
00073 //       static MyClass my_global(absl::base_internal::kLinkerInitialized);
00074 namespace absl {
00075 namespace base_internal {
00076 enum LinkerInitialized {
00077   kLinkerInitialized = 0,
00078 };
00079 }  // namespace base_internal
00080 }  // namespace absl
00081 
00082 // ABSL_FALLTHROUGH_INTENDED
00083 //
00084 // Annotates implicit fall-through between switch labels, allowing a case to
00085 // indicate intentional fallthrough and turn off warnings about any lack of a
00086 // `break` statement. The ABSL_FALLTHROUGH_INTENDED macro should be followed by
00087 // a semicolon and can be used in most places where `break` can, provided that
00088 // no statements exist between it and the next switch label.
00089 //
00090 // Example:
00091 //
00092 //  switch (x) {
00093 //    case 40:
00094 //    case 41:
00095 //      if (truth_is_out_there) {
00096 //        ++x;
00097 //        ABSL_FALLTHROUGH_INTENDED;  // Use instead of/along with annotations
00098 //                                    // in comments
00099 //      } else {
00100 //        return x;
00101 //      }
00102 //    case 42:
00103 //      ...
00104 //
00105 // Notes: when compiled with clang in C++11 mode, the ABSL_FALLTHROUGH_INTENDED
00106 // macro is expanded to the [[clang::fallthrough]] attribute, which is analysed
00107 // when  performing switch labels fall-through diagnostic
00108 // (`-Wimplicit-fallthrough`). See clang documentation on language extensions
00109 // for details:
00110 // http://clang.llvm.org/docs/AttributeReference.html#fallthrough-clang-fallthrough
00111 //
00112 // When used with unsupported compilers, the ABSL_FALLTHROUGH_INTENDED macro
00113 // has no effect on diagnostics. In any case this macro has no effect on runtime
00114 // behavior and performance of code.
00115 #ifdef ABSL_FALLTHROUGH_INTENDED
00116 #error "ABSL_FALLTHROUGH_INTENDED should not be defined."
00117 #endif
00118 
00119 // TODO(zhangxy): Use c++17 standard [[fallthrough]] macro, when supported.
00120 #if defined(__clang__) && defined(__has_warning)
00121 #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
00122 #define ABSL_FALLTHROUGH_INTENDED [[clang::fallthrough]]
00123 #endif
00124 #elif defined(__GNUC__) && __GNUC__ >= 7
00125 #define ABSL_FALLTHROUGH_INTENDED [[gnu::fallthrough]]
00126 #endif
00127 
00128 #ifndef ABSL_FALLTHROUGH_INTENDED
00129 #define ABSL_FALLTHROUGH_INTENDED \
00130   do {                            \
00131   } while (0)
00132 #endif
00133 
00134 // ABSL_DEPRECATED()
00135 //
00136 // Marks a deprecated class, struct, enum, function, method and variable
00137 // declarations. The macro argument is used as a custom diagnostic message (e.g.
00138 // suggestion of a better alternative).
00139 //
00140 // Example:
00141 //
00142 //   class ABSL_DEPRECATED("Use Bar instead") Foo {...};
00143 //   ABSL_DEPRECATED("Use Baz instead") void Bar() {...}
00144 //
00145 // Every usage of a deprecated entity will trigger a warning when compiled with
00146 // clang's `-Wdeprecated-declarations` option. This option is turned off by
00147 // default, but the warnings will be reported by clang-tidy.
00148 #if defined(__clang__) && __cplusplus >= 201103L
00149 #define ABSL_DEPRECATED(message) __attribute__((deprecated(message)))
00150 #endif
00151 
00152 #ifndef ABSL_DEPRECATED
00153 #define ABSL_DEPRECATED(message)
00154 #endif
00155 
00156 // ABSL_BAD_CALL_IF()
00157 //
00158 // Used on a function overload to trap bad calls: any call that matches the
00159 // overload will cause a compile-time error. This macro uses a clang-specific
00160 // "enable_if" attribute, as described at
00161 // http://clang.llvm.org/docs/AttributeReference.html#enable-if
00162 //
00163 // Overloads which use this macro should be bracketed by
00164 // `#ifdef ABSL_BAD_CALL_IF`.
00165 //
00166 // Example:
00167 //
00168 //   int isdigit(int c);
00169 //   #ifdef ABSL_BAD_CALL_IF
00170 //   int isdigit(int c)
00171 //     ABSL_BAD_CALL_IF(c <= -1 || c > 255,
00172 //                       "'c' must have the value of an unsigned char or EOF");
00173 //   #endif // ABSL_BAD_CALL_IF
00174 
00175 #if defined(__clang__)
00176 # if __has_attribute(enable_if)
00177 #  define ABSL_BAD_CALL_IF(expr, msg) \
00178     __attribute__((enable_if(expr, "Bad call trap"), unavailable(msg)))
00179 # endif
00180 #endif
00181 
00182 // ABSL_ASSERT()
00183 //
00184 // In C++11, `assert` can't be used portably within constexpr functions.
00185 // ABSL_ASSERT functions as a runtime assert but works in C++11 constexpr
00186 // functions.  Example:
00187 //
00188 // constexpr double Divide(double a, double b) {
00189 //   return ABSL_ASSERT(b != 0), a / b;
00190 // }
00191 //
00192 // This macro is inspired by
00193 // https://akrzemi1.wordpress.com/2017/05/18/asserts-in-constexpr-functions/
00194 #if defined(NDEBUG)
00195 #define ABSL_ASSERT(expr) \
00196   (false ? static_cast<void>(expr) : static_cast<void>(0))
00197 #else
00198 #define ABSL_ASSERT(expr)                           \
00199   (ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
00200                              : [] { assert(false && #expr); }())  // NOLINT
00201 #endif
00202 
00203 #ifdef ABSL_HAVE_EXCEPTIONS
00204 #define ABSL_INTERNAL_TRY try
00205 #define ABSL_INTERNAL_CATCH_ANY catch (...)
00206 #define ABSL_INTERNAL_RETHROW do { throw; } while (false)
00207 #else  // ABSL_HAVE_EXCEPTIONS
00208 #define ABSL_INTERNAL_TRY if (true)
00209 #define ABSL_INTERNAL_CATCH_ANY else if (false)
00210 #define ABSL_INTERNAL_RETHROW do {} while (false)
00211 #endif  // ABSL_HAVE_EXCEPTIONS
00212 
00213 #endif  // ABSL_BASE_MACROS_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15