summaryrefslogtreecommitdiff
path: root/Source
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2022-12-07 13:19:44 +0000
committerKitware Robot <kwrobot@kitware.com>2022-12-07 08:19:53 -0500
commit3b4337adc79c3744ad7b78b05035b4b460fb5f48 (patch)
tree9057f7ab5b14df7f35589661016810a12c9dcdc3 /Source
parent6bae244ad2a25a95a90dde1aa9047924b5acc530 (diff)
parent232467eb1c0dab9156cd8c4af56aad3959cbee4b (diff)
downloadcmake-3b4337adc79c3744ad7b78b05035b4b460fb5f48.tar.gz
Merge topic 'clang-tidy-export-fixes-dir'
232467eb1c clang-tidy: add <LANG>_CLANG_TIDY_EXPORT_FIXES_DIR property Acked-by: Kitware Robot <kwrobot@kitware.com> Acked-by: buildbot <buildbot@kitware.com> Merge-request: !7982
Diffstat (limited to 'Source')
-rw-r--r--Source/cmGeneratorTarget.cxx18
-rw-r--r--Source/cmGeneratorTarget.h2
-rw-r--r--Source/cmGlobalCommonGenerator.cxx24
-rw-r--r--Source/cmGlobalCommonGenerator.h13
-rw-r--r--Source/cmGlobalNinjaGenerator.cxx4
-rw-r--r--Source/cmGlobalUnixMakefileGenerator3.cxx5
-rw-r--r--Source/cmMakefileTargetGenerator.cxx24
-rw-r--r--Source/cmNinjaTargetGenerator.cxx49
-rw-r--r--Source/cmNinjaTargetGenerator.h5
-rw-r--r--Source/cmTarget.cxx4
-rw-r--r--Source/cmcmd.cxx6
11 files changed, 152 insertions, 2 deletions
diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx
index 25711593b7..7ecdd8781a 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 3cd5e342de..7fa662def8 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 3ae66f0abe..7a44452575 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 fed9ce81b1..fa426743e9 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 f7753da773..cfe4c7c59d 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 70a9d3eec3..30206b5ae4 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 766e9f9032..20cc2c3725 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 3912632097..0807a986cc 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
{
@@ -935,8 +954,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(
@@ -1317,6 +1352,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 b5abb362a9..8bf79867bd 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 70a624d16b..d73e7cf893 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 06bceb4bbe..4303f961b6 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