diff options
author | Kyle Edwards <kyle.edwards@kitware.com> | 2022-11-29 18:39:10 (GMT) |
---|---|---|
committer | Kyle Edwards <kyle.edwards@kitware.com> | 2022-12-06 15:39:29 (GMT) |
commit | 232467eb1c0dab9156cd8c4af56aad3959cbee4b (patch) | |
tree | 2772b01850c143598e642282966401cb976dd421 /Source | |
parent | 7ea665b74da322e96dd3e7f90075143c2957728c (diff) | |
download | CMake-232467eb1c0dab9156cd8c4af56aad3959cbee4b.zip CMake-232467eb1c0dab9156cd8c4af56aad3959cbee4b.tar.gz CMake-232467eb1c0dab9156cd8c4af56aad3959cbee4b.tar.bz2 |
clang-tidy: add <LANG>_CLANG_TIDY_EXPORT_FIXES_DIR property
Fixes: #21362
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmGeneratorTarget.cxx | 18 | ||||
-rw-r--r-- | Source/cmGeneratorTarget.h | 2 | ||||
-rw-r--r-- | Source/cmGlobalCommonGenerator.cxx | 24 | ||||
-rw-r--r-- | Source/cmGlobalCommonGenerator.h | 13 | ||||
-rw-r--r-- | Source/cmGlobalNinjaGenerator.cxx | 4 | ||||
-rw-r--r-- | Source/cmGlobalUnixMakefileGenerator3.cxx | 5 | ||||
-rw-r--r-- | Source/cmMakefileTargetGenerator.cxx | 24 | ||||
-rw-r--r-- | Source/cmNinjaTargetGenerator.cxx | 49 | ||||
-rw-r--r-- | Source/cmNinjaTargetGenerator.h | 5 | ||||
-rw-r--r-- | Source/cmTarget.cxx | 4 | ||||
-rw-r--r-- | Source/cmcmd.cxx | 6 |
11 files changed, 152 insertions, 2 deletions
diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index c80cdb9..123b5e6 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -3726,6 +3726,24 @@ std::string cmGeneratorTarget::GetCreateRuleVariable( return ""; } +//---------------------------------------------------------------------------- +std::string cmGeneratorTarget::GetClangTidyExportFixesDirectory( + const std::string& lang) const +{ + cmValue val = + this->GetProperty(cmStrCat(lang, "_CLANG_TIDY_EXPORT_FIXES_DIR")); + if (!cmNonempty(val)) { + return {}; + } + + std::string path = *val; + if (!cmSystemTools::FileIsFullPath(path)) { + path = + cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(), '/', path); + } + return cmSystemTools::CollapseFullPath(path); +} + namespace { void processIncludeDirectories(cmGeneratorTarget const* tgt, EvaluatedTargetPropertyEntries& entries, diff --git a/Source/cmGeneratorTarget.h b/Source/cmGeneratorTarget.h index 3cd5e34..7fa662d 100644 --- a/Source/cmGeneratorTarget.h +++ b/Source/cmGeneratorTarget.h @@ -490,6 +490,8 @@ public: std::string GetCreateRuleVariable(std::string const& lang, std::string const& config) const; + std::string GetClangTidyExportFixesDirectory(const std::string& lang) const; + private: using ConfigAndLanguage = std::pair<std::string, std::string>; using ConfigAndLanguageToBTStrings = diff --git a/Source/cmGlobalCommonGenerator.cxx b/Source/cmGlobalCommonGenerator.cxx index 3ae66f0..7a44452 100644 --- a/Source/cmGlobalCommonGenerator.cxx +++ b/Source/cmGlobalCommonGenerator.cxx @@ -2,11 +2,14 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmGlobalCommonGenerator.h" +#include <algorithm> #include <memory> #include <utility> #include <cmext/algorithm> +#include <cmsys/Glob.hxx> + #include "cmGeneratorExpression.h" #include "cmGeneratorTarget.h" #include "cmLocalGenerator.h" @@ -14,6 +17,7 @@ #include "cmStateDirectory.h" #include "cmStateSnapshot.h" #include "cmStateTypes.h" +#include "cmStringAlgorithms.h" #include "cmSystemTools.h" #include "cmValue.h" #include "cmake.h" @@ -124,3 +128,23 @@ std::string cmGlobalCommonGenerator::GetEditCacheCommand() const cmValue edit_cmd = cm->GetCacheDefinition("CMAKE_EDIT_COMMAND"); return edit_cmd ? *edit_cmd : std::string(); } + +void cmGlobalCommonGenerator::RemoveUnknownClangTidyExportFixesFiles() const +{ + for (auto const& dir : this->ClangTidyExportFixesDirs) { + cmsys::Glob g; + g.SetRecurse(true); + g.SetListDirs(false); + g.FindFiles(cmStrCat(dir, "/*.yaml")); + for (auto const& file : g.GetFiles()) { + if (!this->ClangTidyExportFixesFiles.count(file) && + !std::any_of(this->ClangTidyExportFixesFiles.begin(), + this->ClangTidyExportFixesFiles.end(), + [&file](const std::string& knownFile) -> bool { + return cmSystemTools::SameFile(file, knownFile); + })) { + cmSystemTools::RemoveFile(file); + } + } + } +} diff --git a/Source/cmGlobalCommonGenerator.h b/Source/cmGlobalCommonGenerator.h index fed9ce8..fa42674 100644 --- a/Source/cmGlobalCommonGenerator.h +++ b/Source/cmGlobalCommonGenerator.h @@ -5,6 +5,7 @@ #include "cmConfigure.h" // IWYU pragma: keep #include <map> +#include <set> #include <string> #include <vector> @@ -42,9 +43,21 @@ public: std::map<std::string, DirectoryTarget> ComputeDirectoryTargets() const; bool IsExcludedFromAllInConfig(const DirectoryTarget::Target& t, const std::string& config); + void AddClangTidyExportFixesDir(const std::string& dir) + { + this->ClangTidyExportFixesDirs.insert(dir); + } + void AddClangTidyExportFixesFile(const std::string& file) + { + this->ClangTidyExportFixesFiles.insert(file); + } protected: virtual bool SupportsDirectConsole() const { return true; } const char* GetEditCacheTargetName() const override { return "edit_cache"; } std::string GetEditCacheCommand() const override; + + std::set<std::string> ClangTidyExportFixesDirs; + std::set<std::string> ClangTidyExportFixesFiles; + void RemoveUnknownClangTidyExportFixesFiles() const; }; diff --git a/Source/cmGlobalNinjaGenerator.cxx b/Source/cmGlobalNinjaGenerator.cxx index 4500f33..75c347e 100644 --- a/Source/cmGlobalNinjaGenerator.cxx +++ b/Source/cmGlobalNinjaGenerator.cxx @@ -592,6 +592,8 @@ void cmGlobalNinjaGenerator::Generate() this->CMakeCacheFile = this->NinjaOutputPath("CMakeCache.txt"); this->DisableCleandead = false; this->DiagnosedCxxModuleNinjaSupport = false; + this->ClangTidyExportFixesDirs.clear(); + this->ClangTidyExportFixesFiles.clear(); this->PolicyCMP0058 = this->LocalGenerators[0]->GetMakefile()->GetPolicyStatus( @@ -632,6 +634,8 @@ void cmGlobalNinjaGenerator::Generate() { this->CleanMetaData(); } + + this->RemoveUnknownClangTidyExportFixesFiles(); } void cmGlobalNinjaGenerator::CleanMetaData() diff --git a/Source/cmGlobalUnixMakefileGenerator3.cxx b/Source/cmGlobalUnixMakefileGenerator3.cxx index 70a9d3e..30206b5 100644 --- a/Source/cmGlobalUnixMakefileGenerator3.cxx +++ b/Source/cmGlobalUnixMakefileGenerator3.cxx @@ -102,6 +102,9 @@ void cmGlobalUnixMakefileGenerator3::Configure() void cmGlobalUnixMakefileGenerator3::Generate() { + this->ClangTidyExportFixesDirs.clear(); + this->ClangTidyExportFixesFiles.clear(); + // first do superclass method this->cmGlobalGenerator::Generate(); @@ -137,6 +140,8 @@ void cmGlobalUnixMakefileGenerator3::Generate() *this->CommandDatabase << "\n]"; this->CommandDatabase.reset(); } + + this->RemoveUnknownClangTidyExportFixesFiles(); } void cmGlobalUnixMakefileGenerator3::AddCXXCompileCommand( diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx index 766e9f9..20cc2c3 100644 --- a/Source/cmMakefileTargetGenerator.cxx +++ b/Source/cmMakefileTargetGenerator.cxx @@ -25,6 +25,7 @@ #include "cmGeneratedFileStream.h" #include "cmGeneratorExpression.h" #include "cmGeneratorTarget.h" +#include "cmGlobalCommonGenerator.h" #include "cmGlobalUnixMakefileGenerator3.h" #include "cmLinkLineComputer.h" // IWYU pragma: keep #include "cmLocalCommonGenerator.h" @@ -1107,8 +1108,29 @@ void cmMakefileTargetGenerator::WriteObjectRuleFiles( } else { driverMode = lang == "C" ? "gcc" : "g++"; } + std::string d = + this->GeneratorTarget->GetClangTidyExportFixesDirectory(lang); + std::string exportFixes; + if (!d.empty()) { + this->GlobalCommonGenerator->AddClangTidyExportFixesDir(d); + std::string fixesFile = cmSystemTools::CollapseFullPath(cmStrCat( + d, '/', + this->LocalGenerator->MaybeRelativeToTopBinDir(cmStrCat( + this->LocalGenerator->GetCurrentBinaryDirectory(), '/', + this->LocalGenerator->GetTargetDirectory( + this->GeneratorTarget), + '/', objectName, ".yaml")))); + this->GlobalCommonGenerator->AddClangTidyExportFixesFile( + fixesFile); + cmSystemTools::MakeDirectory( + cmSystemTools::GetFilenamePath(fixesFile)); + fixesFile = + this->LocalGenerator->MaybeRelativeToCurBinDir(fixesFile); + exportFixes = cmStrCat(";--export-fixes=", fixesFile); + } run_iwyu += this->LocalGenerator->EscapeForShell( - cmStrCat(*tidy, ";--extra-arg-before=--driver-mode=", driverMode)); + cmStrCat(*tidy, ";--extra-arg-before=--driver-mode=", driverMode, + exportFixes)); } if (cmNonempty(cpplint)) { run_iwyu += " --cpplint="; diff --git a/Source/cmNinjaTargetGenerator.cxx b/Source/cmNinjaTargetGenerator.cxx index 85a6fc2..f2f719d 100644 --- a/Source/cmNinjaTargetGenerator.cxx +++ b/Source/cmNinjaTargetGenerator.cxx @@ -27,6 +27,7 @@ #include "cmGeneratedFileStream.h" #include "cmGeneratorExpression.h" #include "cmGeneratorTarget.h" +#include "cmGlobalCommonGenerator.h" #include "cmGlobalNinjaGenerator.h" #include "cmLocalGenerator.h" #include "cmLocalNinjaGenerator.h" @@ -394,6 +395,24 @@ std::string cmNinjaTargetGenerator::GetObjectFilePath( return path; } +std::string cmNinjaTargetGenerator::GetClangTidyReplacementsFilePath( + const std::string& directory, cmSourceFile const* source, + const std::string& config) const +{ + std::string path = this->LocalGenerator->GetHomeRelativeOutputPath(); + if (!path.empty()) { + path += '/'; + } + path = cmStrCat(directory, '/', path); + std::string const& objectName = this->GeneratorTarget->GetObjectName(source); + path = + cmStrCat(std::move(path), + this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget), + this->GetGlobalGenerator()->ConfigDirectory(config), '/', + objectName, ".yaml"); + return path; +} + std::string cmNinjaTargetGenerator::GetPreprocessedFilePath( cmSourceFile const* source, const std::string& config) const { @@ -932,8 +951,24 @@ void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang, } else { driverMode = lang == "C" ? "gcc" : "g++"; } + const bool haveClangTidyExportFixesDir = + !this->GeneratorTarget->GetClangTidyExportFixesDirectory(lang) + .empty(); + std::string exportFixes; + if (haveClangTidyExportFixesDir) { + exportFixes = ";--export-fixes=$CLANG_TIDY_EXPORT_FIXES"; + } run_iwyu += this->GetLocalGenerator()->EscapeForShell( - cmStrCat(*tidy, ";--extra-arg-before=--driver-mode=", driverMode)); + cmStrCat(*tidy, ";--extra-arg-before=--driver-mode=", driverMode, + exportFixes)); + if (haveClangTidyExportFixesDir) { + std::string search = cmStrCat( + this->GetLocalGenerator()->GetState()->UseWindowsShell() ? "" + : "\\", + "$$CLANG_TIDY_EXPORT_FIXES"); + auto loc = run_iwyu.rfind(search); + run_iwyu.replace(loc, search.length(), "$CLANG_TIDY_EXPORT_FIXES"); + } } if (cmNonempty(cpplint)) { run_iwyu += cmStrCat( @@ -1314,6 +1349,18 @@ void cmNinjaTargetGenerator::WriteObjectBuildStatement( } } + std::string d = + this->GeneratorTarget->GetClangTidyExportFixesDirectory(language); + if (!d.empty()) { + this->GlobalCommonGenerator->AddClangTidyExportFixesDir(d); + std::string fixesFile = + this->GetClangTidyReplacementsFilePath(d, source, config); + this->GlobalCommonGenerator->AddClangTidyExportFixesFile(fixesFile); + cmSystemTools::MakeDirectory(cmSystemTools::GetFilenamePath(fixesFile)); + fixesFile = this->ConvertToNinjaPath(fixesFile); + vars["CLANG_TIDY_EXPORT_FIXES"] = fixesFile; + } + if (firstForConfig) { this->ExportObjectCompileCommand( language, sourceFilePath, objectDir, objectFileName, objectFileDir, diff --git a/Source/cmNinjaTargetGenerator.h b/Source/cmNinjaTargetGenerator.h index b5abb36..8bf7986 100644 --- a/Source/cmNinjaTargetGenerator.h +++ b/Source/cmNinjaTargetGenerator.h @@ -134,6 +134,11 @@ protected: std::string GetPreprocessedFilePath(cmSourceFile const* source, const std::string& config) const; + /// @return the clang-tidy replacements file path for the given @a source. + std::string GetClangTidyReplacementsFilePath( + const std::string& directory, cmSourceFile const* source, + const std::string& config) const; + /// @return the dyndep file path for this target. std::string GetDyndepFilePath(std::string const& lang, const std::string& config) const; diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index 70a624d..d73e7cf 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -573,12 +573,14 @@ cmTarget::cmTarget(std::string const& name, cmStateEnums::TargetType type, initProp("NO_SYSTEM_FROM_IMPORTED"); initProp("BUILD_WITH_INSTALL_NAME_DIR"); initProp("C_CLANG_TIDY"); + initProp("C_CLANG_TIDY_EXPORT_FIXES_DIR"); initProp("C_CPPLINT"); initProp("C_CPPCHECK"); initProp("C_INCLUDE_WHAT_YOU_USE"); initProp("C_LINKER_LAUNCHER"); initProp("LINK_WHAT_YOU_USE"); initProp("CXX_CLANG_TIDY"); + initProp("CXX_CLANG_TIDY_EXPORT_FIXES_DIR"); initProp("CXX_CPPLINT"); initProp("CXX_CPPCHECK"); initProp("CXX_INCLUDE_WHAT_YOU_USE"); @@ -600,8 +602,10 @@ cmTarget::cmTarget(std::string const& name, cmStateEnums::TargetType type, initProp("LINK_SEARCH_START_STATIC"); initProp("LINK_SEARCH_END_STATIC"); initProp("OBJC_CLANG_TIDY"); + initProp("OBJC_CLANG_TIDY_EXPORT_FIXES_DIR"); initProp("OBJC_LINKER_LAUNCHER"); initProp("OBJCXX_CLANG_TIDY"); + initProp("OBJCXX_CLANG_TIDY_EXPORT_FIXES_DIR"); initProp("OBJCXX_LINKER_LAUNCHER"); initProp("Swift_LANGUAGE_VERSION"); initProp("Swift_MODULE_DIRECTORY"); diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx index 06bceb4..4303f96 100644 --- a/Source/cmcmd.cxx +++ b/Source/cmcmd.cxx @@ -367,6 +367,12 @@ int HandleTidy(const std::string& runCmd, const std::string& sourceFile, std::vector<std::string> tidy_cmd = cmExpandedList(runCmd, true); tidy_cmd.push_back(sourceFile); + for (auto const& arg : tidy_cmd) { + if (cmHasLiteralPrefix(arg, "--export-fixes=")) { + cmSystemTools::RemoveFile(arg.substr(cmStrLen("--export-fixes="))); + } + } + // clang-tidy supports working out the compile commands from a // compile_commands.json file in a directory given by a "-p" option, or by // passing the compiler command line arguments after --. When the latter |