Features: Add support for C++14 features.

Record the features implemented by GNU 4.9 and Clang 3.4.
This commit is contained in:
Stephen Kelly 2014-05-06 00:21:14 +02:00
parent 3ea9bde845
commit dd043c3f21
23 changed files with 294 additions and 9 deletions

View File

@ -12,6 +12,11 @@ command.
The features known to this version of CMake are:
``cxx_aggregate_default_initializers``
Aggregate default initializers, as defined in N3605_.
.. _N3605: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3605.html
``cxx_alias_templates``
Template aliases, as defined in N2258_.
@ -32,16 +37,31 @@ The features known to this version of CMake are:
.. _N2761: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2761.pdf
``cxx_attribute_deprecated``
``[[deprecated]]`` attribute, as defined in N3760_.
.. _N3760: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3760.html
``cxx_auto_type``
Automatic type deduction, as defined in N1984_.
.. _N1984: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1984.pdf
``cxx_binary_literals``
Binary literals, as defined in N3472_.
.. _N3472: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3472.pdf
``cxx_constexpr``
Constant expressions, as defined in N2235_.
.. _N2235: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2235.pdf
``cxx_contextual_conversions``
Contextual conversions, as defined in N3323_.
.. _N3323: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3323.pdf
``cxx_decltype_incomplete_return_types``
Decltype on incomplete return types, as defined in N3276_.
@ -52,6 +72,11 @@ The features known to this version of CMake are:
.. _N2343: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2343.pdf
``cxx_decltype_auto``
``decltype(auto)`` semantics, as defined in N3638_.
.. _N3638: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3638.html
``cxx_default_function_template_args``
Default template arguments for function templates, as defined in DR226_
@ -77,6 +102,11 @@ The features known to this version of CMake are:
.. _N2346: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm
``cxx_digit_separators``
Digit separators, as defined in N3781_.
.. _N3781: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3781.pdf
``cxx_enum_forward_declarations``
Enum forward declarations, as defined in N2764_.
@ -112,6 +142,11 @@ The features known to this version of CMake are:
.. _N2672: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2672.htm
``cxx_generic_lambdas``
Generic lambdas, ss defined in N3649_.
.. _N3649: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3649.html
``cxx_inheriting_constructors``
Inheriting constructors, as defined in N2540_.
@ -127,6 +162,11 @@ The features known to this version of CMake are:
.. _N2927: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2927.pdf
``cxx_lambda_init_captures``
Initialized lambda captures, as defined in N3648_.
.. _N3648: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3648.html
``cxx_local_type_template_args``
Local and unnamed types as template arguments, as defined in N2657_.
@ -172,6 +212,16 @@ The features known to this version of CMake are:
.. _N2439: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2439.htm
``cxx_relaxed_constexpr``
Relaxed constexpr, as defined in N3652_.
.. _N3652: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3652.html
``cxx_return_type_deduction``
Return type deduction on normal functions, as defined in N3386_.
.. _N3386: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3386.html
``cxx_right_angle_brackets``
Right angle bracket parsing, as defined in N1757_.
@ -227,6 +277,11 @@ The features known to this version of CMake are:
.. _N2765: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2765.pdf
``cxx_variable_templates``
Variable templates, as defined in N3651_.
.. _N3651: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3651.pdf
``cxx_variadic_macros``
Variadic macros, as defined in N1653_.

View File

@ -5,6 +5,7 @@ set(CMAKE_CXX_COMPILER_VERSION "@CMAKE_CXX_COMPILER_VERSION@")
set(CMAKE_CXX_COMPILE_FEATURES "@CMAKE_CXX_COMPILE_FEATURES@")
set(CMAKE_CXX98_COMPILE_FEATURES "@CMAKE_CXX98_COMPILE_FEATURES@")
set(CMAKE_CXX11_COMPILE_FEATURES "@CMAKE_CXX11_COMPILE_FEATURES@")
set(CMAKE_CXX14_COMPILE_FEATURES "@CMAKE_CXX14_COMPILE_FEATURES@")
set(CMAKE_CXX_PLATFORM_ID "@CMAKE_CXX_PLATFORM_ID@")
set(CMAKE_CXX_SIMULATE_ID "@CMAKE_CXX_SIMULATE_ID@")

View File

@ -57,6 +57,7 @@ function(cmake_determine_compile_features lang)
set(CMAKE_CXX98_COMPILE_FEATURES)
set(CMAKE_CXX11_COMPILE_FEATURES)
set(CMAKE_CXX14_COMPILE_FEATURES)
include("${CMAKE_ROOT}/Modules/Internal/FeatureTesting.cmake")
@ -67,7 +68,10 @@ function(cmake_determine_compile_features lang)
return()
endif()
if (CMAKE_CXX11_COMPILE_FEATURES AND CMAKE_CXX98_COMPILE_FEATURES)
if (CMAKE_CXX11_COMPILE_FEATURES AND CMAKE_CXX14_COMPILE_FEATURES)
list(REMOVE_ITEM CMAKE_CXX14_COMPILE_FEATURES ${CMAKE_CXX11_COMPILE_FEATURES})
endif()
if (CMAKE_CXX98_COMPILE_FEATURES AND CMAKE_CXX11_COMPILE_FEATURES)
list(REMOVE_ITEM CMAKE_CXX11_COMPILE_FEATURES ${CMAKE_CXX98_COMPILE_FEATURES})
endif()
@ -75,12 +79,14 @@ function(cmake_determine_compile_features lang)
set(CMAKE_CXX_COMPILE_FEATURES
${CMAKE_CXX98_COMPILE_FEATURES}
${CMAKE_CXX11_COMPILE_FEATURES}
${CMAKE_CXX14_COMPILE_FEATURES}
)
endif()
set(CMAKE_CXX_COMPILE_FEATURES ${CMAKE_CXX_COMPILE_FEATURES} PARENT_SCOPE)
set(CMAKE_CXX98_COMPILE_FEATURES ${CMAKE_CXX98_COMPILE_FEATURES} PARENT_SCOPE)
set(CMAKE_CXX11_COMPILE_FEATURES ${CMAKE_CXX11_COMPILE_FEATURES} PARENT_SCOPE)
set(CMAKE_CXX14_COMPILE_FEATURES ${CMAKE_CXX14_COMPILE_FEATURES} PARENT_SCOPE)
message(STATUS "Detecting ${lang} compile features - done")
endif()

View File

@ -7,7 +7,9 @@ set(testable_features
cxx_alignas
cxx_attributes
cxx_auto_type
cxx_binary_literals
cxx_constexpr
cxx_contextual_conversions
cxx_decltype
cxx_decltype_incomplete_return_types
cxx_default_function_template_args
@ -25,6 +27,8 @@ set(testable_features
cxx_range_for
cxx_raw_string_literals
cxx_reference_qualified_functions
cxx_relaxed_constexpr
cxx_return_type_deduction
cxx_rvalue_references
cxx_static_assert
cxx_strong_enums
@ -32,6 +36,7 @@ set(testable_features
cxx_unicode_literals
cxx_unrestricted_unions
cxx_user_literals
cxx_variable_templates
cxx_variadic_templates
)
@ -43,12 +48,24 @@ endforeach()
unset(testable_features)
set(_cmake_feature_test_cxx_aggregate_default_initializers "${_cmake_oldestSupported} && __has_feature(cxx_aggregate_nsdmi)")
set(_cmake_feature_test_cxx_trailing_return_types "${_cmake_oldestSupported} && __has_feature(cxx_trailing_return)")
set(_cmake_feature_test_cxx_alignof "${_cmake_oldestSupported} && __has_feature(cxx_alignas)")
set(_cmake_feature_test_cxx_final "${_cmake_oldestSupported} && __has_feature(cxx_override_control)")
set(_cmake_feature_test_cxx_override "${_cmake_oldestSupported} && __has_feature(cxx_override_control)")
set(_cmake_feature_test_cxx_uniform_initialization "${_cmake_oldestSupported} && __has_feature(cxx_generalized_initializers)")
set(_cmake_feature_test_cxx_defaulted_move_initializers "${_cmake_oldestSupported} && __has_feature(cxx_defaulted_functions)")
set(_cmake_feature_test_cxx_lambda_init_captures "${_cmake_oldestSupported} && __has_feature(cxx_init_captures)")
set(Clang34_CXX14 "((__clang_major__ * 100) + __clang_minor__) >= 304 && __cplusplus > 201103L")
# http://llvm.org/bugs/show_bug.cgi?id=19242
set(_cmake_feature_test_cxx_attribute_deprecated "${Clang34_CXX14}")
# http://llvm.org/bugs/show_bug.cgi?id=19698
set(_cmake_feature_test_cxx_decltype_auto "${Clang34_CXX14}")
set(_cmake_feature_test_cxx_digit_separators "${Clang34_CXX14}")
# http://llvm.org/bugs/show_bug.cgi?id=19674
set(_cmake_feature_test_cxx_generic_lambdas "${Clang34_CXX14}")
# TODO: Should be supported by Clang 3.1
set(Clang31_CXX11 "${_cmake_oldestSupported} && __cplusplus >= 201103L")

View File

@ -2,6 +2,26 @@
# Reference: http://gcc.gnu.org/projects/cxx0x.html
set(_cmake_oldestSupported "(__GNUC__ * 100 + __GNUC_MINOR__) >= 407")
set(GNU49_CXX14 "(__GNUC__ * 100 + __GNUC_MINOR__) >= 409 && __cplusplus > 201103L")
set(_cmake_feature_test_cxx_contextual_conversions "${GNU49_CXX14}")
set(_cmake_feature_test_cxx_attribute_deprecated "${GNU49_CXX14}")
set(_cmake_feature_test_cxx_decltype_auto "${GNU49_CXX14}")
set(_cmake_feature_test_cxx_digit_separators "${GNU49_CXX14}")
set(_cmake_feature_test_cxx_generic_lambdas "${GNU49_CXX14}")
set(_cmake_feature_test_cxx_lambda_init_captures "${GNU49_CXX14}")
# GNU 4.3 supports binary literals as an extension, but may warn about
# use of extensions prior to GNU 4.9
# http://stackoverflow.com/questions/16334024/difference-between-gcc-binary-literals-and-c14-ones
set(_cmake_feature_test_cxx_binary_literals "${GNU49_CXX14}")
# The feature below is documented as available in GNU 4.8 (by implementing an
# earlier draft of the standard paper), but that version of the compiler
# does not set __cplusplus to a value greater than 201103L until GNU 4.9:
# http://gcc.gnu.org/onlinedocs/gcc-4.8.2/cpp/Standard-Predefined-Macros.html#Standard-Predefined-Macros
# http://gcc.gnu.org/onlinedocs/gcc-4.9.0/cpp/Standard-Predefined-Macros.html#Standard-Predefined-Macros
# So, CMake only reports availability for it with GNU 4.9 or later.
set(_cmake_feature_test_cxx_return_type_deduction "${GNU49_CXX14}")
# Introduced in GCC 4.8.1
set(GNU481_CXX11 "((__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40801) && __cplusplus >= 201103L")
set(_cmake_feature_test_cxx_decltype_incomplete_return_types "${GNU481_CXX11}")

View File

@ -24,6 +24,11 @@ elseif(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.3)
set(CMAKE_CXX11_EXTENSION_COMPILE_OPTION "-std=gnu++0x")
endif()
if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
set(CMAKE_CXX14_STANDARD_COMPILE_OPTION "-std=c++1y")
set(CMAKE_CXX14_EXTENSION_COMPILE_OPTION "-std=gnu++1y")
endif()
set(CMAKE_CXX_STANDARD_DEFAULT 98)
macro(cmake_record_cxx_compile_features)
@ -31,8 +36,14 @@ macro(cmake_record_cxx_compile_features)
record_compiler_features(CXX "-std=${std_version}" ${list})
endmacro()
set(_result 0)
if (UNIX AND NOT APPLE AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
_get_gcc_features(c++1y CMAKE_CXX14_COMPILE_FEATURES)
endif()
if (UNIX AND NOT APPLE AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.7)
_get_gcc_features(c++11 CMAKE_CXX11_COMPILE_FEATURES)
if (_result EQUAL 0)
_get_gcc_features(c++11 CMAKE_CXX11_COMPILE_FEATURES)
endif()
if (_result EQUAL 0)
_get_gcc_features(c++98 CMAKE_CXX98_COMPILE_FEATURES)
endif()

View File

@ -2222,6 +2222,7 @@ AddCompilerRequirementFlag(std::string &flags, cmTarget* target,
if (langStdMap.empty())
{
// Maintain sorted order, most recent first.
langStdMap["CXX"].push_back("14");
langStdMap["CXX"].push_back("11");
langStdMap["CXX"].push_back("98");

View File

@ -4992,6 +4992,7 @@ static const char * const C_STANDARDS[] = {
static const char * const CXX_STANDARDS[] = {
"98"
, "11"
, "14"
};
//----------------------------------------------------------------------------
@ -5214,7 +5215,8 @@ bool cmMakefile::HaveCxxFeatureAvailable(cmTarget const* target,
{
bool needCxx98 = false;
bool needCxx11 = false;
this->CheckNeededCxxLanguage(feature, needCxx98, needCxx11);
bool needCxx14 = false;
this->CheckNeededCxxLanguage(feature, needCxx98, needCxx11, needCxx14);
const char *existingCxxStandard = target->GetProperty("CXX_STANDARD");
if (!existingCxxStandard)
@ -5257,7 +5259,8 @@ bool cmMakefile::HaveCxxFeatureAvailable(cmTarget const* target,
//----------------------------------------------------------------------------
void cmMakefile::CheckNeededCxxLanguage(const std::string& feature,
bool& needCxx98,
bool& needCxx11) const
bool& needCxx11,
bool& needCxx14) const
{
if (const char *propCxx98 =
this->GetDefinition("CMAKE_CXX98_COMPILE_FEATURES"))
@ -5273,6 +5276,13 @@ void cmMakefile::CheckNeededCxxLanguage(const std::string& feature,
cmSystemTools::ExpandListArgument(propCxx11, props);
needCxx11 = std::find(props.begin(), props.end(), feature) != props.end();
}
if (const char *propCxx14 =
this->GetDefinition("CMAKE_CXX14_COMPILE_FEATURES"))
{
std::vector<std::string> props;
cmSystemTools::ExpandListArgument(propCxx14, props);
needCxx14 = std::find(props.begin(), props.end(), feature) != props.end();
}
}
//----------------------------------------------------------------------------
@ -5282,8 +5292,9 @@ AddRequiredTargetCxxFeature(cmTarget *target,
{
bool needCxx98 = false;
bool needCxx11 = false;
bool needCxx14 = false;
this->CheckNeededCxxLanguage(feature, needCxx98, needCxx11);
this->CheckNeededCxxLanguage(feature, needCxx98, needCxx11, needCxx14);
const char *existingCxxStandard = target->GetProperty("CXX_STANDARD");
if (existingCxxStandard)
@ -5306,8 +5317,16 @@ AddRequiredTargetCxxFeature(cmTarget *target,
bool setCxx98 = needCxx98 && !existingCxxStandard;
bool setCxx11 = needCxx11 && !existingCxxStandard;
bool setCxx14 = needCxx14 && !existingCxxStandard;
if (needCxx11 && existingCxxStandard && existingCxxIt <
if (needCxx14 && existingCxxStandard && existingCxxIt <
std::find_if(cmArrayBegin(CXX_STANDARDS),
cmArrayEnd(CXX_STANDARDS),
cmStrCmp("14")))
{
setCxx14 = true;
}
else if (needCxx11 && existingCxxStandard && existingCxxIt <
std::find_if(cmArrayBegin(CXX_STANDARDS),
cmArrayEnd(CXX_STANDARDS),
cmStrCmp("11")))
@ -5322,7 +5341,11 @@ AddRequiredTargetCxxFeature(cmTarget *target,
setCxx98 = true;
}
if (setCxx11)
if (setCxx14)
{
target->SetProperty("CXX_STANDARD", "14");
}
else if (setCxx11)
{
target->SetProperty("CXX_STANDARD", "11");
}

View File

@ -1121,7 +1121,7 @@ private:
void CheckNeededCLanguage(const std::string& feature, bool& needC90,
bool& needC99, bool& needC11) const;
void CheckNeededCxxLanguage(const std::string& feature, bool& needCxx98,
bool& needCxx11) const;
bool& needCxx11, bool& needCxx14) const;
bool HaveCFeatureAvailable(cmTarget const* target,
const std::string& feature) const;

View File

@ -465,19 +465,25 @@ private:
F(c_variadic_macros)
#define FOR_EACH_CXX_FEATURE(F) \
F(cxx_aggregate_default_initializers) \
F(cxx_alias_templates) \
F(cxx_alignas) \
F(cxx_alignof) \
F(cxx_attributes) \
F(cxx_attribute_deprecated) \
F(cxx_auto_type) \
F(cxx_binary_literals) \
F(cxx_constexpr) \
F(cxx_contextual_conversions) \
F(cxx_decltype) \
F(cxx_decltype_auto) \
F(cxx_decltype_incomplete_return_types) \
F(cxx_default_function_template_args) \
F(cxx_defaulted_functions) \
F(cxx_defaulted_move_initializers) \
F(cxx_delegating_constructors) \
F(cxx_deleted_functions) \
F(cxx_digit_separators) \
F(cxx_enum_forward_declarations) \
F(cxx_explicit_conversions) \
F(cxx_extended_friend_declarations) \
@ -485,9 +491,11 @@ private:
F(cxx_final) \
F(cxx_func_identifier) \
F(cxx_generalized_initializers) \
F(cxx_generic_lambdas) \
F(cxx_inheriting_constructors) \
F(cxx_inline_namespaces) \
F(cxx_lambdas) \
F(cxx_lambda_init_captures) \
F(cxx_local_type_template_args) \
F(cxx_long_long_type) \
F(cxx_noexcept) \
@ -497,6 +505,8 @@ private:
F(cxx_range_for) \
F(cxx_raw_string_literals) \
F(cxx_reference_qualified_functions) \
F(cxx_relaxed_constexpr) \
F(cxx_return_type_deduction) \
F(cxx_right_angle_brackets) \
F(cxx_rvalue_references) \
F(cxx_sizeof_member) \
@ -509,6 +519,7 @@ private:
F(cxx_uniform_initialization) \
F(cxx_unrestricted_unions) \
F(cxx_user_literals) \
F(cxx_variable_templates) \
F(cxx_variadic_macros) \
F(cxx_variadic_templates)

View File

@ -37,11 +37,24 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL GNU
cxx_alignof
)
endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL GNU
AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
# GNU prior to 4.9 does not set any preprocessor define to distinguish
# c++1y from c++11, so CMake does not support c++1y features before GNU 4.9.
list(REMOVE_ITEM CXX_non_features
# GNU 4.8 knows cxx_attributes, but doesn't know [[deprecated]]
# and warns that it is unknown and ignored.
cxx_attribute_deprecated
cxx_binary_literals
cxx_lambda_init_captures
cxx_return_type_deduction
)
endif()
set(C_ext c)
set(C_standard_flag 11)
set(CXX_ext cpp)
set(CXX_standard_flag 11)
set(CXX_standard_flag 14)
foreach(lang CXX C)
if (CMAKE_${lang}_COMPILE_FEATURES)
foreach(feature ${${lang}_non_features})

View File

@ -0,0 +1,9 @@
struct X { int i, j, k = 42; };
int someFunc()
{
X a[] = { 1, 2, 3, 4, 5, 6 };
X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
return a[0].k == b[0].k && a[1].k == b[1].k ? 0 : 1;
}

View File

@ -0,0 +1,11 @@
[[deprecated]]
int foo()
{
return 0;
}
int someFunc()
{
return foo();
}

View File

@ -0,0 +1,6 @@
int someFunc()
{
int i = 0b101;
return i - 5;
}

View File

@ -0,0 +1,33 @@
#define assert(E) if(!(E)) return 1;
template<class T>
class zero_init
{
public:
zero_init( )
: val( static_cast<T>(0) ) { }
zero_init( T val ) : val( val )
{ }
operator T & ( ) { return val; }
operator T ( ) const { return val; }
private:
T val;
};
int someFunc()
{
zero_init<int*> p; assert( p == 0 );
p = new int(7);
assert( *p == 7 );
delete p;
zero_init<int> i; assert( i == 0 );
i = 7;
assert( i == 7 );
switch( i ) { }
int *vp = new int[i];
return 0;
}

View File

@ -0,0 +1,6 @@
int someFunc(int argc, char**)
{
decltype(auto) i = argc;
return 0;
}

View File

@ -0,0 +1,6 @@
int someFunc()
{
int one_thousand = 1'000;
return one_thousand - 1000;
}

View File

@ -0,0 +1,6 @@
int someFunc()
{
auto identity = [](auto a) { return a; };
return identity(0);
}

View File

@ -0,0 +1,6 @@
int someFunc()
{
int a = 0;
return [b = static_cast<int&&>(a)]() { return b; }();
}

View File

@ -0,0 +1,23 @@
struct X {
constexpr X() : n(5) {
n *= 2;
}
int n;
};
constexpr int g(const int (&is)[4]) {
X x;
int r = x.n;
for (int i = 0; i < 5; ++i)
r += i;
for (auto& i : is)
r += i;
return r;
}
int someFunc()
{
constexpr int k3 = g({ 4, 5, 6, 7 });
return k3 - 42;
}

View File

@ -0,0 +1,10 @@
auto sum(int a, int b)
{
return a+b;
}
int someFunc()
{
return sum(3, -3);
}

View File

@ -0,0 +1,10 @@
template<typename T>
constexpr T pi = T(3.1415926535897932385);
int someFunc()
{
auto pi_int = pi<int>;
auto pi_float = pi<float>;
return pi_int - 3;
}

View File

@ -38,6 +38,7 @@ CMAKE_CXX11_EXTENSION_COMPILE_OPTION == "${CMAKE_CXX11_EXTENSION_COMPILE_OPTION}
CMAKE_CXX_COMPILE_FEATURES == "${CMAKE_CXX_COMPILE_FEATURES}"
CMAKE_CXX98_COMPILE_FEATURES == "${CMAKE_CXX98_COMPILE_FEATURES}"
CMAKE_CXX11_COMPILE_FEATURES == "${CMAKE_CXX11_COMPILE_FEATURES}"
CMAKE_CXX14_COMPILE_FEATURES == "${CMAKE_CXX14_COMPILE_FEATURES}"
// C shared library flag
CMAKE_SHARED_LIBRARY_C_FLAGS == "${CMAKE_SHARED_LIBRARY_C_FLAGS}"