From 06e2f8d8fa3a5335e8d91de9d5fe5db2ee4ea379 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Wed, 6 Jul 2022 13:24:32 +0200 Subject: [PATCH] CppEditor: Always use "clang:std=" for specifying the language version ... when in clang-cl mode. We observe strange behavior when keeping the MSVC /std:c++xx syntax, namely that files that are not specified in the compilation database appear to get parsed without a language version. The problem does not occur when passing the flag via "clang:". Fixes: QTCREATORBUG-27764 Change-Id: I0035e5fef6dfd37fbbe396b538f38fc29b23923b Reviewed-by: Reviewed-by: David Schulz --- src/plugins/cppeditor/compileroptionsbuilder.cpp | 14 ++++++-------- .../cppeditor/compileroptionsbuilder_test.cpp | 6 +++--- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/plugins/cppeditor/compileroptionsbuilder.cpp b/src/plugins/cppeditor/compileroptionsbuilder.cpp index abe6a758e98..505cb4ee0a1 100644 --- a/src/plugins/cppeditor/compileroptionsbuilder.cpp +++ b/src/plugins/cppeditor/compileroptionsbuilder.cpp @@ -473,16 +473,16 @@ void CompilerOptionsBuilder::addLanguageVersionAndExtensions() default: break; case LanguageVersion::CXX14: - option = "/std:c++14"; + option = "-clang:std=c++14"; break; case LanguageVersion::CXX17: - option = "/std:c++17"; + option = "-clang:std=c++17"; break; case LanguageVersion::CXX20: - option = "/std:c++20"; + option = "-clang:std=c++20"; break; case LanguageVersion::CXX2b: - option = "/std:c++latest"; + option = "-clang:std=c++2b"; break; } @@ -910,12 +910,10 @@ void CompilerOptionsBuilder::evaluateCompilerFlags() theOption[0] = '-'; } - // Clang-cl (as of Clang 12) frontend doesn't know about -std:c++20 - // but the clang front end knows about -std=c++20 - // https://github.com/llvm/llvm-project/blob/release/12.x/clang/lib/Driver/ToolChains/Clang.cpp#L5855 if (toolChain == ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID || toolChain == ProjectExplorer::Constants::CLANG_CL_TOOLCHAIN_TYPEID) { - theOption.replace("-std:c++20", "-clang:-std=c++20"); + theOption.replace("-std:c++latest", "-clang:-std=c++2b"); + theOption.replace("-std:c++", "-clang:-std=c++"); } m_compilerFlags.flags.append(theOption); diff --git a/src/plugins/cppeditor/compileroptionsbuilder_test.cpp b/src/plugins/cppeditor/compileroptionsbuilder_test.cpp index 1fca97f592e..65d18291849 100644 --- a/src/plugins/cppeditor/compileroptionsbuilder_test.cpp +++ b/src/plugins/cppeditor/compileroptionsbuilder_test.cpp @@ -192,7 +192,7 @@ void CompilerOptionsBuilderTest::testLanguageVersionIsExplicitlySetIfNotProvided UseTweakedHeaderPaths::No, UseLanguageDefines::Yes}; compilerOptionsBuilder.build(ProjectFile::CXXSource, UsePrecompiledHeaders::No); - QVERIFY(compilerOptionsBuilder.options().contains("/std:c++17")); + QVERIFY(compilerOptionsBuilder.options().contains("-clang:std=c++17")); } void CompilerOptionsBuilderTest::testAddWordWidth() @@ -633,7 +633,7 @@ void CompilerOptionsBuilderTest::testBuildAllOptionsMsvc() [&t](const QString &o) { return o.contains(t.toNative("wrappedQtHeaders/QtCore")); }); QCOMPARE(compilerOptionsBuilder.options(), (QStringList{"-nostdinc", "-nostdinc++", "--driver-mode=cl", "/Zs", "-m64", - "--target=x86_64-apple-darwin10", "/TP", "/std:c++17", + "--target=x86_64-apple-darwin10", "/TP", "-clang:std=c++17", "-fms-compatibility-version=19.00", "-DprojectFoo=projectBar", "-D__FUNCSIG__=\"void __cdecl someLegalAndLongishFunctionNameThatWorksAroundQTCREATORBUG-24580(void)\"", "-D__FUNCTION__=\"someLegalAndLongishFunctionNameThatWorksAroundQTCREATORBUG-24580\"", @@ -662,7 +662,7 @@ void CompilerOptionsBuilderTest::testBuildAllOptionsMsvcWithExceptions() [&t](const QString &o) { return o.contains(t.toNative("wrappedQtHeaders/QtCore")); }); QCOMPARE(compilerOptionsBuilder.options(), (QStringList{"-nostdinc", "-nostdinc++", "--driver-mode=cl", "/Zs", "-m64", - "--target=x86_64-apple-darwin10", "/TP", "/std:c++17", "-fcxx-exceptions", + "--target=x86_64-apple-darwin10", "/TP", "-clang:std=c++17", "-fcxx-exceptions", "-fexceptions", "-fms-compatibility-version=19.00", "-DprojectFoo=projectBar", "-D__FUNCSIG__=\"void __cdecl someLegalAndLongishFunctionNameThatWorksAroundQTCREATORBUG-24580(void)\"",