summaryrefslogtreecommitdiffstats
path: root/Tests/CompileFeatures
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2022-11-17 15:15:55 (GMT)
committerBrad King <brad.king@kitware.com>2022-11-18 16:44:52 (GMT)
commit914571a04249ee561a30309197d8e1c1cacbe18b (patch)
tree81bcf00395d68dd29fe3099d4ba588ed7701ede1 /Tests/CompileFeatures
parentad16ae5c70778e6bfd6d30b17a3ad9b248b2a569 (diff)
downloadCMake-914571a04249ee561a30309197d8e1c1cacbe18b.zip
CMake-914571a04249ee561a30309197d8e1c1cacbe18b.tar.gz
CMake-914571a04249ee561a30309197d8e1c1cacbe18b.tar.bz2
Place language standard flags just after CMAKE_<LANG>_FLAGS
Previously we added the language standard flag near the end of all options, even after those added by `add_compile_options` and friends. However, on some compilers such as MSVC, the `-std` flag may reset defaults for flags that precede it on the command line. Move the language standard flag to before all other flags that CMake adds for other abstractions, and before those added by `add_compile_options`. `CMAKE_<LANG>_FLAGS` should still precede the language flags though, because they are meant to be treated as language-wide modifications to the compiler defaults, similar to `$CC $CFLAGS`. Fixes: #23860 Fixes: #24170
Diffstat (limited to 'Tests/CompileFeatures')
-rw-r--r--Tests/CompileFeatures/CMakeLists.txt13
-rw-r--r--Tests/CompileFeatures/msvc_permissive.cxx9
2 files changed, 22 insertions, 0 deletions
diff --git a/Tests/CompileFeatures/CMakeLists.txt b/Tests/CompileFeatures/CMakeLists.txt
index f3d3a73..17f4408 100644
--- a/Tests/CompileFeatures/CMakeLists.txt
+++ b/Tests/CompileFeatures/CMakeLists.txt
@@ -374,3 +374,16 @@ else()
target_link_libraries(CompileFeaturesGenex3 PRIVATE std_11_iface)
target_compile_definitions(CompileFeaturesGenex3 PRIVATE ${genex_test_defs} ALLOW_LATER_STANDARDS=1)
endif()
+
+if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC"
+ AND (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.30
+ # The MSVC 14.29.30133 toolset supports C++20,
+ # but MSBuild puts the flags in the wrong order.
+ OR (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.29.30129 AND NOT CMAKE_GENERATOR MATCHES "Visual Studio")
+ )
+ )
+ add_library(msvc_permissive msvc_permissive.cxx)
+ target_compile_features(msvc_permissive PRIVATE cxx_std_20)
+ # The `-std:c++20` flag implies `-permissive-`. Test passing `-permissive` afterward.
+ target_compile_options(msvc_permissive PRIVATE -permissive)
+endif()
diff --git a/Tests/CompileFeatures/msvc_permissive.cxx b/Tests/CompileFeatures/msvc_permissive.cxx
new file mode 100644
index 0000000..a8f2ff3
--- /dev/null
+++ b/Tests/CompileFeatures/msvc_permissive.cxx
@@ -0,0 +1,9 @@
+#if !defined(_MSVC_LANG) || _MSVC_LANG < 202002L
+# error "This source must be compiled with MSVC as C++20 or later."
+#endif
+// Test a construct that is allowed by MSVC only with 'cl -permissive'.
+enum class X
+{
+ Y = 1
+};
+int array[X::Y];