diff --git a/Modules/Compiler/Clang.cmake b/Modules/Compiler/Clang.cmake index e85cdb2a18..f834f7a783 100644 --- a/Modules/Compiler/Clang.cmake +++ b/Modules/Compiler/Clang.cmake @@ -255,6 +255,11 @@ macro(__compiler_clang_cxx_standards lang) endif() if(CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL "17.0") + # This version of clang-cl does not have a -std:c++23 flag. + # Pass the standard through to the underlying clang directly. + # Note that cmVisualStudio10TargetGenerator::ComputeClOptions + # has a special case to map this back to -std:c++latest in .vcxproj + # files that also have C sources. set(CMAKE_${lang}23_STANDARD_COMPILE_OPTION "-clang:-std=c++23") set(CMAKE_${lang}23_EXTENSION_COMPILE_OPTION "-clang:-std=c++23") set(CMAKE_${lang}_STANDARD_LATEST 23) diff --git a/Source/cmVisualStudio10TargetGenerator.cxx b/Source/cmVisualStudio10TargetGenerator.cxx index 95ff7e6e03..f7515f1fb3 100644 --- a/Source/cmVisualStudio10TargetGenerator.cxx +++ b/Source/cmVisualStudio10TargetGenerator.cxx @@ -3480,6 +3480,20 @@ bool cmVisualStudio10TargetGenerator::ComputeClOptions( } } + if (isCXXwithC) { + // Modules/Compiler/Clang.cmake has a special case for clang-cl versions + // that do not have a -std:c++23 flag to pass the standard through to the + // underlying clang directly. Unfortunately that flag applies to all + // sources in a single .vcxproj file, so if we have C sources too then we + // cannot use it. Map it back to -std::c++latest, even though that might + // end up enabling C++26 or later, so it does not apply to C sources. + static const std::string kClangStdCxx23 = "-clang:-std=c++23"; + std::string::size_type p = flags.find(kClangStdCxx23); + if (p != std::string::npos) { + flags.replace(p, kClangStdCxx23.size(), "-std:c++latest"); + } + } + clOptions.Parse(flags); clOptions.Parse(defineFlags); std::vector targetDefines; diff --git a/Tests/CompileFeatures/CMakeLists.txt b/Tests/CompileFeatures/CMakeLists.txt index 7fc5e85cd7..29174e5b92 100644 --- a/Tests/CompileFeatures/CMakeLists.txt +++ b/Tests/CompileFeatures/CMakeLists.txt @@ -32,6 +32,12 @@ foreach(lang C CXX CUDA HIP) endforeach() endforeach() +if(("23" IN_LIST CMake_TEST_CXX_STANDARDS OR CMAKE_CXX23_STANDARD_COMPILE_OPTION) + AND ("11" IN_LIST CMake_TEST_C_STANDARDS OR CMAKE_C11_STANDARD_COMPILE_OPTION)) + add_library(test_cxx_std_23_with_c_std_11 OBJECT cxx_std_23.cpp c_std_11.c) + target_compile_features(test_cxx_std_23_with_c_std_11 PRIVATE cxx_std_23 c_std_11) +endif() + macro(run_test feature lang) if (${feature} IN_LIST CMAKE_${lang}_COMPILE_FEATURES) add_library(test_${feature} OBJECT ${feature}.${ext_${lang}})