From ccfe1b0b401863a8ac9ef07bb88ef970af587edc Mon Sep 17 00:00:00 2001 From: PigeonF Date: Wed, 8 Sep 2021 19:08:28 +0200 Subject: IWYU: Add `--driver-mode=cl` when applicable We already do this for `clang-tidy`. Fixes: #16554 --- Modules/Compiler/Clang-C.cmake | 1 + Modules/Compiler/Clang-CXX.cmake | 1 + Modules/Compiler/Intel-C.cmake | 1 + Modules/Compiler/Intel-CXX.cmake | 1 + Modules/Compiler/IntelLLVM-C.cmake | 1 + Modules/Compiler/IntelLLVM-CXX.cmake | 1 + Modules/Compiler/MSVC-C.cmake | 1 + Modules/Compiler/MSVC-CXX.cmake | 1 + Source/cmMakefileTargetGenerator.cxx | 20 +++++++++++++++++++- Source/cmNinjaTargetGenerator.cxx | 22 ++++++++++++++++++++-- 10 files changed, 47 insertions(+), 3 deletions(-) diff --git a/Modules/Compiler/Clang-C.cmake b/Modules/Compiler/Clang-C.cmake index cf493d7..1b765ad 100644 --- a/Modules/Compiler/Clang-C.cmake +++ b/Modules/Compiler/Clang-C.cmake @@ -9,6 +9,7 @@ endif() if("x${CMAKE_C_COMPILER_FRONTEND_VARIANT}" STREQUAL "xMSVC") set(CMAKE_C_COMPILE_OPTIONS_EXPLICIT_LANGUAGE -TC) set(CMAKE_C_CLANG_TIDY_DRIVER_MODE "cl") + set(CMAKE_C_INCLUDE_WHAT_YOU_USE_DRIVER_MODE "cl") if((NOT DEFINED CMAKE_DEPENDS_USE_COMPILER OR CMAKE_DEPENDS_USE_COMPILER) AND CMAKE_GENERATOR MATCHES "Makefiles|WMake" AND CMAKE_DEPFILE_FLAGS_C) diff --git a/Modules/Compiler/Clang-CXX.cmake b/Modules/Compiler/Clang-CXX.cmake index 98828e0..84b05d7 100644 --- a/Modules/Compiler/Clang-CXX.cmake +++ b/Modules/Compiler/Clang-CXX.cmake @@ -22,6 +22,7 @@ endif() if("x${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}" STREQUAL "xMSVC") set(CMAKE_CXX_CLANG_TIDY_DRIVER_MODE "cl") + set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE_DRIVER_MODE "cl") if((NOT DEFINED CMAKE_DEPENDS_USE_COMPILER OR CMAKE_DEPENDS_USE_COMPILER) AND CMAKE_GENERATOR MATCHES "Makefiles" AND CMAKE_DEPFILE_FLAGS_CXX) diff --git a/Modules/Compiler/Intel-C.cmake b/Modules/Compiler/Intel-C.cmake index ead9069..9884b58 100644 --- a/Modules/Compiler/Intel-C.cmake +++ b/Modules/Compiler/Intel-C.cmake @@ -17,6 +17,7 @@ if("x${CMAKE_C_SIMULATE_ID}" STREQUAL "xMSVC") set(CMAKE_C_COMPILE_OPTIONS_EXPLICIT_LANGUAGE -TC) set(CMAKE_C_CLANG_TIDY_DRIVER_MODE "cl") + set(CMAKE_C_INCLUDE_WHAT_YOU_USE_DRIVER_MODE "cl") if (NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 16.0.0) set(CMAKE_C11_STANDARD_COMPILE_OPTION "-Qstd=c11") diff --git a/Modules/Compiler/Intel-CXX.cmake b/Modules/Compiler/Intel-CXX.cmake index 37f339a..7c9cca9 100644 --- a/Modules/Compiler/Intel-CXX.cmake +++ b/Modules/Compiler/Intel-CXX.cmake @@ -16,6 +16,7 @@ endif() if("x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC") set(CMAKE_CXX_CLANG_TIDY_DRIVER_MODE "cl") + set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE_DRIVER_MODE "cl") if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.0.0) set(CMAKE_CXX20_STANDARD_COMPILE_OPTION "-Qstd=c++20") diff --git a/Modules/Compiler/IntelLLVM-C.cmake b/Modules/Compiler/IntelLLVM-C.cmake index d69d064..d7346f6 100644 --- a/Modules/Compiler/IntelLLVM-C.cmake +++ b/Modules/Compiler/IntelLLVM-C.cmake @@ -4,6 +4,7 @@ __compiler_intel_llvm(C) if("x${CMAKE_C_COMPILER_FRONTEND_VARIANT}" STREQUAL "xMSVC") set(CMAKE_C_COMPILE_OPTIONS_EXPLICIT_LANGUAGE -TC) set(CMAKE_C_CLANG_TIDY_DRIVER_MODE "cl") + set(CMAKE_C_INCLUDE_WHAT_YOU_USE_DRIVER_MODE "cl") if((NOT DEFINED CMAKE_DEPENDS_USE_COMPILER OR CMAKE_DEPENDS_USE_COMPILER) AND CMAKE_GENERATOR MATCHES "Makefiles|WMake" AND CMAKE_DEPFILE_FLAGS_C) diff --git a/Modules/Compiler/IntelLLVM-CXX.cmake b/Modules/Compiler/IntelLLVM-CXX.cmake index 9799888..cae1f11 100644 --- a/Modules/Compiler/IntelLLVM-CXX.cmake +++ b/Modules/Compiler/IntelLLVM-CXX.cmake @@ -4,6 +4,7 @@ __compiler_intel_llvm(CXX) if("x${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}" STREQUAL "xMSVC") set(CMAKE_CXX_COMPILE_OPTIONS_EXPLICIT_LANGUAGE -TP) set(CMAKE_CXX_CLANG_TIDY_DRIVER_MODE "cl") + set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE_DRIVER_MODE "cl") if((NOT DEFINED CMAKE_DEPENDS_USE_COMPILER OR CMAKE_DEPENDS_USE_COMPILER) AND CMAKE_GENERATOR MATCHES "Makefiles|WMake" AND CMAKE_DEPFILE_FLAGS_CXX) diff --git a/Modules/Compiler/MSVC-C.cmake b/Modules/Compiler/MSVC-C.cmake index 73cca36..4ba1eea 100644 --- a/Modules/Compiler/MSVC-C.cmake +++ b/Modules/Compiler/MSVC-C.cmake @@ -29,6 +29,7 @@ endif() set(CMAKE_C_COMPILE_OPTIONS_EXPLICIT_LANGUAGE -TC) set(CMAKE_C_CLANG_TIDY_DRIVER_MODE "cl") +set(CMAKE_C_INCLUDE_WHAT_YOU_USE_DRIVER_MODE "cl") # There are no C compiler modes so we hard-code the known compiler supported # features. Override the default macro for this special case. Pretend that diff --git a/Modules/Compiler/MSVC-CXX.cmake b/Modules/Compiler/MSVC-CXX.cmake index 09fe851..9bb7722 100644 --- a/Modules/Compiler/MSVC-CXX.cmake +++ b/Modules/Compiler/MSVC-CXX.cmake @@ -4,6 +4,7 @@ include(Compiler/CMakeCommonCompilerMacros) set(CMAKE_CXX_CLANG_TIDY_DRIVER_MODE "cl") +set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE_DRIVER_MODE "cl") if ((CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.0.24215.1 AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.10) OR diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx index c83a7ab..70b6d27 100644 --- a/Source/cmMakefileTargetGenerator.cxx +++ b/Source/cmMakefileTargetGenerator.cxx @@ -999,7 +999,25 @@ void cmMakefileTargetGenerator::WriteObjectRuleFiles( } if (cmNonempty(iwyu)) { run_iwyu += " --iwyu="; - run_iwyu += this->LocalGenerator->EscapeForShell(*iwyu); + + // Only add --driver-mode if it is not already specified, as adding + // it unconditionally might override a user-specified driver-mode + if (iwyu.Get()->find("--driver-mode=") == std::string::npos) { + cmProp p = this->Makefile->GetDefinition( + cmStrCat("CMAKE_", lang, "_INCLUDE_WHAT_YOU_USE_DRIVER_MODE")); + std::string driverMode; + + if (cmNonempty(p)) { + driverMode = *p; + } else { + driverMode = lang == "C" ? "gcc" : "g++"; + } + + run_iwyu += this->LocalGenerator->EscapeForShell( + cmStrCat(*iwyu, ";--driver-mode=", driverMode)); + } else { + run_iwyu += this->LocalGenerator->EscapeForShell(*iwyu); + } } if (cmNonempty(tidy)) { run_iwyu += " --tidy="; diff --git a/Source/cmNinjaTargetGenerator.cxx b/Source/cmNinjaTargetGenerator.cxx index 1b6b834..f1ea229 100644 --- a/Source/cmNinjaTargetGenerator.cxx +++ b/Source/cmNinjaTargetGenerator.cxx @@ -874,8 +874,26 @@ void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang, compilerLauncher.clear(); } if (cmNonempty(iwyu)) { - run_iwyu += cmStrCat(" --iwyu=", - this->GetLocalGenerator()->EscapeForShell(*iwyu)); + run_iwyu += " --iwyu="; + + // Only add --driver-mode if it is not already specified, as adding + // it unconditionally might override a user-specified driver-mode + if (iwyu.Get()->find("--driver-mode=") == std::string::npos) { + cmProp p = this->Makefile->GetDefinition( + cmStrCat("CMAKE_", lang, "_INCLUDE_WHAT_YOU_USE_DRIVER_MODE")); + std::string driverMode; + + if (cmNonempty(p)) { + driverMode = *p; + } else { + driverMode = lang == "C" ? "gcc" : "g++"; + } + + run_iwyu += this->LocalGenerator->EscapeForShell( + cmStrCat(*iwyu, ";--driver-mode=", driverMode)); + } else { + run_iwyu += this->LocalGenerator->EscapeForShell(*iwyu); + } } if (cmNonempty(tidy)) { run_iwyu += " --tidy="; -- cgit v0.12