cxx/test/support/test_macros.h
Eric Fiselier 00f4a49b0b [libcxx] Allow use of <atomic> in C++03. Try 3.
Summary:
After putting this question up on cfe-dev I have decided that it would be best to allow the use of `<atomic>` in C++03. Although static initialization is a concern the syntax required to get it is C++11 only. Meaning that C++11 constant static initialization cannot silently break in C++03, it will always cause a syntax error. Furthermore `ATOMIC_VAR_INIT` and `ATOMIC_FLAG_INIT` remain defined in C++03 even though they cannot be used because C++03 usages will cause better error messages.

The main change in this patch is to replace `__has_feature(cxx_atomic)`, which only returns true when C++ >= 11, to `__has_extension(c_atomic)` which returns true whenever clang supports the required atomic builtins.


This patch adds the following macros:
* `_LIBCPP_HAS_C_ATOMIC_IMP`      - Defined on clang versions which provide the C `_Atomic` keyword.
* `_LIBCPP_HAS_GCC_ATOMIC_IMP` - Defined on GCC > 4.7. We must use the fallback atomic implementation.
* `_LIBCPP_HAS_NO_ATOMIC_HEADER` - Defined when it is not safe to include `<atomic>`.

`_LIBCPP_HAS_C_ATOMIC_IMP` and `_LIBCPP_HAS_GCC_ATOMIC_IMP` are mutually exclusive, only one should be defined. If neither is defined then `<atomic>` is not implemented and including `<atomic>` will issue an error.

Reviewers: chandlerc, jroelofs, mclow.lists

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D11555

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@245463 91177308-0d34-0410-b5e6-96231b3b80d8
2015-08-19 17:21:46 +00:00

100 lines
2.5 KiB
C++

// -*- C++ -*-
//===---------------------------- test_macros.h ---------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef SUPPORT_TEST_MACROS_HPP
#define SUPPORT_TEST_MACROS_HPP
#define TEST_CONCAT1(X, Y) X##Y
#define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y)
#ifdef __has_feature
#define TEST_HAS_FEATURE(X) __has_feature(X)
#else
#define TEST_HAS_FEATURE(X) 0
#endif
#ifdef __has_extension
#define TEST_HAS_EXTENSION(X) __has_extension(X)
#else
#define TEST_HAS_EXTENSION(X) 0
#endif
/* Make a nice name for the standard version */
#if __cplusplus <= 199711L
# define TEST_STD_VER 3
#elif __cplusplus <= 201103L
# define TEST_STD_VER 11
#elif __cplusplus <= 201402L
# define TEST_STD_VER 14
#else
# define TEST_STD_VER 99 // greater than current standard
#endif
/* Features that were introduced in C++11 */
#if TEST_STD_VER >= 11
#define TEST_HAS_RVALUE_REFERENCES
#define TEST_HAS_VARIADIC_TEMPLATES
#define TEST_HAS_INITIALIZER_LISTS
#define TEST_HAS_BASIC_CONSTEXPR
#endif
/* Features that were introduced in C++14 */
#if TEST_STD_VER >= 14
#define TEST_HAS_EXTENDED_CONSTEXPR
#define TEST_HAS_VARIABLE_TEMPLATES
#endif
/* Features that were introduced after C++14 */
#if TEST_STD_VER > 14
#endif
#if TEST_HAS_EXTENSION(cxx_decltype) || TEST_STD_VER >= 11
#define TEST_DECLTYPE(T) decltype(T)
#else
#define TEST_DECLTYPE(T) __typeof__(T)
#endif
#if TEST_STD_VER >= 11
#define TEST_CONSTEXPR constexpr
#define TEST_NOEXCEPT noexcept
#else
#define TEST_CONSTEXPR
#define TEST_NOEXCEPT
#endif
#if TEST_HAS_EXTENSION(cxx_static_assert) || TEST_STD_VER >= 11
# define TEST_STATIC_ASSERT(Expr, Msg) static_assert(Expr, Msg)
#else
# define TEST_STATIC_ASSERT(Expr, Msg) \
typedef ::test_detail::static_assert_check<sizeof( \
::test_detail::static_assert_incomplete_test<(Expr)>)> \
TEST_CONCAT(test_assert, __LINE__)
#
#endif
namespace test_detail {
template <bool> struct static_assert_incomplete_test;
template <> struct static_assert_incomplete_test<true> {};
template <unsigned> struct static_assert_check {};
} // end namespace test_detail
#if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cxx_rtti)
#define TEST_HAS_NO_RTTI
#endif
#if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cxx_exceptions)
#define TEST_HAS_NO_EXCEPTIONS
#endif
#endif // SUPPORT_TEST_MACROS_HPP