From 52039e8b71b7199ad388534b53d8e5cfa16ee421 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Fri, 13 Feb 2015 20:05:57 +0800 Subject: [PATCH] Add feature testing codes in CMakeLists.txt for future use. Doesn't work though. --- CMakeLists.txt | 45 ++++++++++++++++++++++++++++++++++++++++++++- format.h | 35 ++++++++++++++++------------------- test/CMakeLists.txt | 2 +- 3 files changed, 61 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 32a03adc..d765423a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,7 +80,7 @@ if (BIICODE) endif () add_library(format ${FMT_SOURCES}) -if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") +if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") set_target_properties(format PROPERTIES COMPILE_FLAGS "-Wall -Wextra -Wshadow -pedantic") if (CPP11_FLAG) @@ -117,6 +117,49 @@ if (NOT FMT_VARIADIC_TEMPLATES) add_definitions(-DGTEST_LANG_CXX11=0) endif () +# g++ 4.6 supports nullptr with __cplusplus==1 +# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=1773 +check_cxx_source_compiles(" + int main(){int*p=nullptr;}" FMT_NULLPTR) +if (FMT_NULLPTR) + add_definitions(-DFMT_USE_NULLPTR=1) +endif () + +check_cxx_source_compiles(" + struct C{ + C()=delete; + C(const C&)=delete; + C& operator=(const C&)=delete; + }; + int main(){}" FMT_DELETED_FUNCTIONS) +if (FMT_DELETED_FUNCTIONS) + add_definitions(-DFMT_USE_DELETED_FUNCTIONS=1) +endif () + +check_cxx_source_compiles(" + void f() noexcept {} + int main(){ f(); }" FMT_BASIC_NOEXCEPT_SUPPORT) +if (FMT_BASIC_NOEXCEPT_SUPPORT) + add_definitions(-DFMT_USE_NOEXCEPT=1) +endif () + +check_cxx_source_compiles(" + #include + struct MoveOnly { + MoveOnly() {} + MoveOnly(MoveOnly&&) {} + private: + MoveOnly(const MoveOnly&); + }; + int main() { + MoveOnly a; + MoveOnly b(std::move(a)); + MoveOnly c(MoveOnly()); + }" FMT_RVALUE_REFERENCES) +if (FMT_RVALUE_REFERENCES) + add_definitions(-DFMT_USE_RVALUE_REFERENCES=1) +endif () + # GTest doesn't detect with clang. if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") target_compile_definitions(gmock PUBLIC GTEST_USE_OWN_TR1_TUPLE=1) diff --git a/format.h b/format.h index 8a923b57..a837f6a1 100644 --- a/format.h +++ b/format.h @@ -115,46 +115,43 @@ #if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || \ (FMT_GCC_VERSION >= 408 && __cplusplus >= 201103L) # define FMT_NOEXCEPT noexcept -# define FMT_NOEXCEPT_EXPR(expr) noexcept(expr) #else # define FMT_NOEXCEPT throw() -# define FMT_NOEXCEPT_EXPR(expr) #endif // A macro to disallow the copy constructor and operator= functions // This should be used in the private: declarations for a class -#if FMT_HAS_FEATURE(cxx_deleted_functions)\ +#if FMT_USE_DELETED_FUNCTIONS || FMT_HAS_FEATURE(cxx_deleted_functions)\ || (FMT_GCC_VERSION >= 404 && __cplusplus >= 201103L)\ || (_MSC_VER >= 1800) #define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \ TypeName(const TypeName&) = delete; \ - void operator=(const TypeName&) = delete + TypeName& operator=(const TypeName&) = delete #else #define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \ TypeName(const TypeName&); \ - void operator=(const TypeName&) + TypeName& operator=(const TypeName&) #endif -#if FMT_HAS_FEATURE(cxx_nullptr)\ +#if FMT_USE_NULLPTR || FMT_HAS_FEATURE(cxx_nullptr)\ || (FMT_GCC_VERSION >= 406 && __cplusplus >= 201103L)\ || (_MSC_VER >= 1600) // Use nullptr #else // Simulate it namespace std { -class nullptr_t { -public: - template - operator T*() const { - return 0; - } - template - operator T C::*() const - { - return 0; - } -private: - void operator&() const; +class nullptr_t { +public: + template + operator T*() const { + return 0; + } + template + operator T C::*() const { + return 0; + } +private: + void operator&() const; }; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5ab3c2eb..8efe915f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -19,7 +19,7 @@ add_fmt_test(format-test) add_fmt_test(format-impl-test CUSTOM_LINK) add_fmt_test(printf-test) foreach (target format-test printf-test) - if (CMAKE_COMPILER_IS_GNUCXX) + if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") set_target_properties(${target} PROPERTIES COMPILE_FLAGS "-Wall -Wextra -pedantic -Wno-long-long -Wno-variadic-macros") endif ()