2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Copyright 2017 The Abseil Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2019-03-08 16:27:53 +01:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// File: macros.h
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// This header file defines the set of language macros used within Abseil code.
|
|
|
|
// For the set of macros used to determine supported compilers and platforms,
|
|
|
|
// see absl/base/config.h instead.
|
|
|
|
//
|
|
|
|
// This code is compiled directly on many platforms, including client
|
|
|
|
// platforms like Windows, Mac, and embedded systems. Before making
|
|
|
|
// any changes here, make sure that you're not breaking any platforms.
|
|
|
|
|
|
|
|
#ifndef ABSL_BASE_MACROS_H_
|
|
|
|
#define ABSL_BASE_MACROS_H_
|
|
|
|
|
2017-10-24 19:37:49 +02:00
|
|
|
#include <cassert>
|
2017-09-19 22:54:40 +02:00
|
|
|
#include <cstddef>
|
|
|
|
|
2019-12-02 19:41:53 +01:00
|
|
|
#include "absl/base/attributes.h"
|
2020-03-23 21:16:18 +01:00
|
|
|
#include "absl/base/config.h"
|
2019-05-17 19:51:37 +02:00
|
|
|
#include "absl/base/optimization.h"
|
2017-09-19 22:54:40 +02:00
|
|
|
#include "absl/base/port.h"
|
|
|
|
|
|
|
|
// ABSL_ARRAYSIZE()
|
|
|
|
//
|
2018-05-09 22:57:56 +02:00
|
|
|
// Returns the number of elements in an array as a compile-time constant, which
|
|
|
|
// can be used in defining new arrays. If you use this macro on a pointer by
|
2017-09-19 22:54:40 +02:00
|
|
|
// mistake, you will get a compile-time error.
|
2018-05-09 22:57:56 +02:00
|
|
|
#define ABSL_ARRAYSIZE(array) \
|
|
|
|
(sizeof(::absl::macros_internal::ArraySizeHelper(array)))
|
|
|
|
|
2017-09-19 22:54:40 +02:00
|
|
|
namespace absl {
|
2019-12-10 18:03:32 +01:00
|
|
|
ABSL_NAMESPACE_BEGIN
|
2017-09-19 22:54:40 +02:00
|
|
|
namespace macros_internal {
|
2018-05-09 22:57:56 +02:00
|
|
|
// Note: this internal template function declaration is used by ABSL_ARRAYSIZE.
|
|
|
|
// The function doesn't need a definition, as we only use its type.
|
2017-09-19 22:54:40 +02:00
|
|
|
template <typename T, size_t N>
|
2018-02-27 22:38:47 +01:00
|
|
|
auto ArraySizeHelper(const T (&array)[N]) -> char (&)[N];
|
2017-09-19 22:54:40 +02:00
|
|
|
} // namespace macros_internal
|
2019-12-10 18:03:32 +01:00
|
|
|
ABSL_NAMESPACE_END
|
2017-09-19 22:54:40 +02:00
|
|
|
} // namespace absl
|
|
|
|
|
|
|
|
// ABSL_FALLTHROUGH_INTENDED
|
|
|
|
//
|
|
|
|
// Annotates implicit fall-through between switch labels, allowing a case to
|
|
|
|
// indicate intentional fallthrough and turn off warnings about any lack of a
|
|
|
|
// `break` statement. The ABSL_FALLTHROUGH_INTENDED macro should be followed by
|
|
|
|
// a semicolon and can be used in most places where `break` can, provided that
|
|
|
|
// no statements exist between it and the next switch label.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// switch (x) {
|
|
|
|
// case 40:
|
|
|
|
// case 41:
|
|
|
|
// if (truth_is_out_there) {
|
|
|
|
// ++x;
|
|
|
|
// ABSL_FALLTHROUGH_INTENDED; // Use instead of/along with annotations
|
|
|
|
// // in comments
|
|
|
|
// } else {
|
|
|
|
// return x;
|
|
|
|
// }
|
|
|
|
// case 42:
|
|
|
|
// ...
|
|
|
|
//
|
|
|
|
// Notes: when compiled with clang in C++11 mode, the ABSL_FALLTHROUGH_INTENDED
|
|
|
|
// macro is expanded to the [[clang::fallthrough]] attribute, which is analysed
|
|
|
|
// when performing switch labels fall-through diagnostic
|
|
|
|
// (`-Wimplicit-fallthrough`). See clang documentation on language extensions
|
|
|
|
// for details:
|
2020-01-13 20:12:06 +01:00
|
|
|
// https://clang.llvm.org/docs/AttributeReference.html#fallthrough-clang-fallthrough
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// When used with unsupported compilers, the ABSL_FALLTHROUGH_INTENDED macro
|
|
|
|
// has no effect on diagnostics. In any case this macro has no effect on runtime
|
|
|
|
// behavior and performance of code.
|
|
|
|
#ifdef ABSL_FALLTHROUGH_INTENDED
|
|
|
|
#error "ABSL_FALLTHROUGH_INTENDED should not be defined."
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// TODO(zhangxy): Use c++17 standard [[fallthrough]] macro, when supported.
|
|
|
|
#if defined(__clang__) && defined(__has_warning)
|
|
|
|
#if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
|
|
|
|
#define ABSL_FALLTHROUGH_INTENDED [[clang::fallthrough]]
|
|
|
|
#endif
|
|
|
|
#elif defined(__GNUC__) && __GNUC__ >= 7
|
|
|
|
#define ABSL_FALLTHROUGH_INTENDED [[gnu::fallthrough]]
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ABSL_FALLTHROUGH_INTENDED
|
|
|
|
#define ABSL_FALLTHROUGH_INTENDED \
|
|
|
|
do { \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ABSL_DEPRECATED()
|
|
|
|
//
|
|
|
|
// Marks a deprecated class, struct, enum, function, method and variable
|
|
|
|
// declarations. The macro argument is used as a custom diagnostic message (e.g.
|
|
|
|
// suggestion of a better alternative).
|
|
|
|
//
|
2019-09-19 18:27:34 +02:00
|
|
|
// Examples:
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// class ABSL_DEPRECATED("Use Bar instead") Foo {...};
|
2019-09-19 18:27:34 +02:00
|
|
|
//
|
|
|
|
// ABSL_DEPRECATED("Use Baz() instead") void Bar() {...}
|
|
|
|
//
|
|
|
|
// template <typename T>
|
|
|
|
// ABSL_DEPRECATED("Use DoThat() instead")
|
|
|
|
// void DoThis();
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Every usage of a deprecated entity will trigger a warning when compiled with
|
|
|
|
// clang's `-Wdeprecated-declarations` option. This option is turned off by
|
2017-09-24 17:20:48 +02:00
|
|
|
// default, but the warnings will be reported by clang-tidy.
|
2018-01-22 22:10:49 +01:00
|
|
|
#if defined(__clang__) && __cplusplus >= 201103L
|
2017-09-19 22:54:40 +02:00
|
|
|
#define ABSL_DEPRECATED(message) __attribute__((deprecated(message)))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ABSL_DEPRECATED
|
|
|
|
#define ABSL_DEPRECATED(message)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ABSL_BAD_CALL_IF()
|
|
|
|
//
|
|
|
|
// Used on a function overload to trap bad calls: any call that matches the
|
|
|
|
// overload will cause a compile-time error. This macro uses a clang-specific
|
|
|
|
// "enable_if" attribute, as described at
|
2020-01-13 20:12:06 +01:00
|
|
|
// https://clang.llvm.org/docs/AttributeReference.html#enable-if
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Overloads which use this macro should be bracketed by
|
|
|
|
// `#ifdef ABSL_BAD_CALL_IF`.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// int isdigit(int c);
|
|
|
|
// #ifdef ABSL_BAD_CALL_IF
|
|
|
|
// int isdigit(int c)
|
|
|
|
// ABSL_BAD_CALL_IF(c <= -1 || c > 255,
|
|
|
|
// "'c' must have the value of an unsigned char or EOF");
|
|
|
|
// #endif // ABSL_BAD_CALL_IF
|
2019-12-02 19:41:53 +01:00
|
|
|
#if ABSL_HAVE_ATTRIBUTE(enable_if)
|
|
|
|
#define ABSL_BAD_CALL_IF(expr, msg) \
|
|
|
|
__attribute__((enable_if(expr, "Bad call trap"), unavailable(msg)))
|
2017-09-19 22:54:40 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// ABSL_ASSERT()
|
|
|
|
//
|
|
|
|
// In C++11, `assert` can't be used portably within constexpr functions.
|
|
|
|
// ABSL_ASSERT functions as a runtime assert but works in C++11 constexpr
|
|
|
|
// functions. Example:
|
|
|
|
//
|
|
|
|
// constexpr double Divide(double a, double b) {
|
|
|
|
// return ABSL_ASSERT(b != 0), a / b;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// This macro is inspired by
|
|
|
|
// https://akrzemi1.wordpress.com/2017/05/18/asserts-in-constexpr-functions/
|
|
|
|
#if defined(NDEBUG)
|
2019-04-04 17:13:57 +02:00
|
|
|
#define ABSL_ASSERT(expr) \
|
|
|
|
(false ? static_cast<void>(expr) : static_cast<void>(0))
|
2017-09-19 22:54:40 +02:00
|
|
|
#else
|
2019-04-04 17:13:57 +02:00
|
|
|
#define ABSL_ASSERT(expr) \
|
|
|
|
(ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
|
2018-06-13 20:44:15 +02:00
|
|
|
: [] { assert(false && #expr); }()) // NOLINT
|
2017-09-19 22:54:40 +02:00
|
|
|
#endif
|
|
|
|
|
2020-03-23 21:16:18 +01:00
|
|
|
// `ABSL_INTERNAL_HARDENING_ABORT()` controls how `ABSL_HARDENING_ASSERT()`
|
|
|
|
// aborts the program in release mode (when NDEBUG is defined). The
|
|
|
|
// implementation should abort the program as quickly as possible and ideally it
|
|
|
|
// should not be possible to ignore the abort request.
|
2020-03-26 16:48:01 +01:00
|
|
|
#if (ABSL_HAVE_BUILTIN(__builtin_trap) && \
|
|
|
|
ABSL_HAVE_BUILTIN(__builtin_unreachable)) || \
|
2020-03-23 21:16:18 +01:00
|
|
|
(defined(__GNUC__) && !defined(__clang__))
|
|
|
|
#define ABSL_INTERNAL_HARDENING_ABORT() \
|
|
|
|
do { \
|
|
|
|
__builtin_trap(); \
|
|
|
|
__builtin_unreachable(); \
|
|
|
|
} while (false)
|
|
|
|
#else
|
|
|
|
#define ABSL_INTERNAL_HARDENING_ABORT() abort()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ABSL_HARDENING_ASSERT()
|
|
|
|
//
|
2020-03-26 16:48:01 +01:00
|
|
|
// `ABSL_HARDENING_ASSERT()` is like `ABSL_ASSERT()`, but used to implement
|
2020-03-23 21:16:18 +01:00
|
|
|
// runtime assertions that should be enabled in hardened builds even when
|
|
|
|
// `NDEBUG` is defined.
|
|
|
|
//
|
2020-03-26 16:48:01 +01:00
|
|
|
// When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT()` is identical to
|
2020-03-23 21:16:18 +01:00
|
|
|
// `ABSL_ASSERT()`.
|
|
|
|
//
|
|
|
|
// See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on
|
|
|
|
// hardened mode.
|
|
|
|
#if ABSL_OPTION_HARDENED == 1 && defined(NDEBUG)
|
|
|
|
#define ABSL_HARDENING_ASSERT(expr) \
|
|
|
|
(ABSL_PREDICT_TRUE((expr)) ? static_cast<void>(0) \
|
|
|
|
: [] { ABSL_INTERNAL_HARDENING_ABORT(); }())
|
|
|
|
#else
|
|
|
|
#define ABSL_HARDENING_ASSERT(expr) ABSL_ASSERT(expr)
|
|
|
|
#endif
|
|
|
|
|
2018-10-29 23:53:34 +01:00
|
|
|
#ifdef ABSL_HAVE_EXCEPTIONS
|
|
|
|
#define ABSL_INTERNAL_TRY try
|
|
|
|
#define ABSL_INTERNAL_CATCH_ANY catch (...)
|
|
|
|
#define ABSL_INTERNAL_RETHROW do { throw; } while (false)
|
|
|
|
#else // ABSL_HAVE_EXCEPTIONS
|
|
|
|
#define ABSL_INTERNAL_TRY if (true)
|
|
|
|
#define ABSL_INTERNAL_CATCH_ANY else if (false)
|
|
|
|
#define ABSL_INTERNAL_RETHROW do {} while (false)
|
|
|
|
#endif // ABSL_HAVE_EXCEPTIONS
|
|
|
|
|
2017-09-19 22:54:40 +02:00
|
|
|
#endif // ABSL_BASE_MACROS_H_
|