2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2014-09-25 11:11:58 +02:00
|
|
|
|
2018-01-17 15:08:30 +01:00
|
|
|
#include "clangtoolssettings.h"
|
2014-09-25 11:11:58 +02:00
|
|
|
|
2018-01-17 15:08:30 +01:00
|
|
|
#include "clangtoolsconstants.h"
|
2019-10-01 16:53:01 +02:00
|
|
|
#include "clangtoolsutils.h"
|
2022-08-15 14:28:21 +02:00
|
|
|
#include "executableinfo.h"
|
2014-09-25 11:11:58 +02:00
|
|
|
|
|
|
|
|
#include <coreplugin/icore.h>
|
2021-08-30 10:58:08 +02:00
|
|
|
#include <cppeditor/clangdiagnosticconfig.h>
|
2022-05-19 14:48:09 +02:00
|
|
|
#include <cppeditor/clangdiagnosticconfigsmodel.h>
|
2021-08-30 10:58:08 +02:00
|
|
|
#include <cppeditor/cppcodemodelsettings.h>
|
|
|
|
|
#include <cppeditor/cpptoolsreuse.h>
|
2019-09-25 15:46:15 +02:00
|
|
|
|
|
|
|
|
#include <utils/algorithm.h>
|
2014-09-25 11:11:58 +02:00
|
|
|
|
|
|
|
|
#include <QThread>
|
|
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
using namespace CppEditor;
|
2021-08-12 09:19:55 +02:00
|
|
|
using namespace Utils;
|
2019-09-25 15:46:15 +02:00
|
|
|
|
2019-09-13 10:49:14 +02:00
|
|
|
namespace ClangTools {
|
|
|
|
|
namespace Internal {
|
2014-09-25 11:11:58 +02:00
|
|
|
|
2021-08-12 09:39:02 +02:00
|
|
|
|
|
|
|
|
const char parallelJobsKey[] = "ParallelJobs";
|
2023-04-11 12:49:21 +02:00
|
|
|
const char preferConfigFileKey[] = "PreferConfigFile";
|
2021-08-12 09:39:02 +02:00
|
|
|
const char buildBeforeAnalysisKey[] = "BuildBeforeAnalysis";
|
|
|
|
|
const char analyzeOpenFilesKey[] = "AnalyzeOpenFiles";
|
|
|
|
|
const char oldDiagnosticConfigIdKey[] = "diagnosticConfigId";
|
|
|
|
|
|
|
|
|
|
static Id defaultDiagnosticId()
|
2019-09-25 15:46:15 +02:00
|
|
|
{
|
|
|
|
|
return ClangTools::Constants::DIAG_CONFIG_TIDY_AND_CLAZY;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 10:49:14 +02:00
|
|
|
RunSettings::RunSettings()
|
2019-09-25 15:46:15 +02:00
|
|
|
: m_diagnosticConfigId(defaultDiagnosticId())
|
|
|
|
|
, m_parallelJobs(qMax(0, QThread::idealThreadCount() / 2))
|
2014-09-25 11:11:58 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-25 13:19:26 +02:00
|
|
|
void RunSettings::fromMap(const Store &map, const Key &prefix)
|
2014-09-25 11:11:58 +02:00
|
|
|
{
|
2021-08-12 09:39:02 +02:00
|
|
|
m_diagnosticConfigId = Id::fromSetting(map.value(prefix + diagnosticConfigIdKey));
|
2019-09-13 10:49:14 +02:00
|
|
|
m_parallelJobs = map.value(prefix + parallelJobsKey).toInt();
|
2023-04-11 12:49:21 +02:00
|
|
|
m_preferConfigFile = map.value(prefix + preferConfigFileKey).toBool();
|
2019-09-13 10:49:14 +02:00
|
|
|
m_buildBeforeAnalysis = map.value(prefix + buildBeforeAnalysisKey).toBool();
|
2020-07-22 14:52:06 +02:00
|
|
|
m_analyzeOpenFiles = map.value(prefix + analyzeOpenFilesKey).toBool();
|
2014-09-25 11:11:58 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-25 13:19:26 +02:00
|
|
|
void RunSettings::toMap(Store &map, const Key &prefix) const
|
2014-09-25 11:11:58 +02:00
|
|
|
{
|
2019-09-13 10:49:14 +02:00
|
|
|
map.insert(prefix + diagnosticConfigIdKey, m_diagnosticConfigId.toSetting());
|
|
|
|
|
map.insert(prefix + parallelJobsKey, m_parallelJobs);
|
2023-04-11 12:49:21 +02:00
|
|
|
map.insert(prefix + preferConfigFileKey, m_preferConfigFile);
|
2019-09-13 10:49:14 +02:00
|
|
|
map.insert(prefix + buildBeforeAnalysisKey, m_buildBeforeAnalysis);
|
2020-07-22 14:52:06 +02:00
|
|
|
map.insert(prefix + analyzeOpenFilesKey, m_analyzeOpenFiles);
|
2014-09-25 11:11:58 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-12 09:39:02 +02:00
|
|
|
Id RunSettings::diagnosticConfigId() const
|
2019-09-25 15:46:15 +02:00
|
|
|
{
|
2019-10-01 16:53:01 +02:00
|
|
|
if (!diagnosticConfigsModel().hasConfigWithId(m_diagnosticConfigId))
|
|
|
|
|
return defaultDiagnosticId();
|
|
|
|
|
return m_diagnosticConfigId;
|
2019-09-25 15:46:15 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-22 14:52:06 +02:00
|
|
|
bool RunSettings::operator==(const RunSettings &other) const
|
|
|
|
|
{
|
|
|
|
|
return m_diagnosticConfigId == other.m_diagnosticConfigId
|
|
|
|
|
&& m_parallelJobs == other.m_parallelJobs
|
2023-04-11 12:49:21 +02:00
|
|
|
&& m_preferConfigFile == other.m_preferConfigFile
|
2020-07-22 14:52:06 +02:00
|
|
|
&& m_buildBeforeAnalysis == other.m_buildBeforeAnalysis
|
|
|
|
|
&& m_analyzeOpenFiles == other.m_analyzeOpenFiles;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 12:49:21 +02:00
|
|
|
bool RunSettings::hasConfigFileForSourceFile(const Utils::FilePath &sourceFile) const
|
|
|
|
|
{
|
|
|
|
|
if (!preferConfigFile())
|
|
|
|
|
return false;
|
|
|
|
|
for (FilePath parentDir = sourceFile.parentDir(); !parentDir.isEmpty();
|
|
|
|
|
parentDir = parentDir.parentDir()) {
|
|
|
|
|
if (parentDir.resolvePath(QLatin1String(".clang-tidy")).isReadableFile())
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 10:49:14 +02:00
|
|
|
ClangToolsSettings *ClangToolsSettings::instance()
|
2018-05-02 16:13:01 +02:00
|
|
|
{
|
2019-09-13 10:49:14 +02:00
|
|
|
static ClangToolsSettings instance;
|
|
|
|
|
return &instance;
|
2018-05-02 16:13:01 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-14 12:53:50 +02:00
|
|
|
ClangToolsSettings::ClangToolsSettings()
|
|
|
|
|
{
|
|
|
|
|
setSettingsGroup(Constants::SETTINGS_ID);
|
|
|
|
|
|
|
|
|
|
clangTidyExecutable.setSettingsKey("ClangTidyExecutable");
|
|
|
|
|
|
|
|
|
|
clazyStandaloneExecutable.setSettingsKey("ClazyStandaloneExecutable");
|
|
|
|
|
|
|
|
|
|
readSettings();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-25 13:19:26 +02:00
|
|
|
static Store convertToMapFromVersionBefore410(QSettings *s)
|
2018-05-02 16:13:01 +02:00
|
|
|
{
|
2019-09-13 10:49:14 +02:00
|
|
|
const char oldParallelJobsKey[] = "simultaneousProcesses";
|
|
|
|
|
const char oldBuildBeforeAnalysisKey[] = "buildBeforeAnalysis";
|
2018-05-02 16:13:01 +02:00
|
|
|
|
2023-08-25 13:19:26 +02:00
|
|
|
Store map;
|
2019-09-13 10:49:14 +02:00
|
|
|
map.insert(diagnosticConfigIdKey, s->value(oldDiagnosticConfigIdKey));
|
|
|
|
|
map.insert(parallelJobsKey, s->value(oldParallelJobsKey));
|
|
|
|
|
map.insert(buildBeforeAnalysisKey, s->value(oldBuildBeforeAnalysisKey));
|
2018-05-07 15:40:14 +02:00
|
|
|
|
2019-09-13 10:49:14 +02:00
|
|
|
s->remove(oldDiagnosticConfigIdKey);
|
|
|
|
|
s->remove(oldParallelJobsKey);
|
|
|
|
|
s->remove(oldBuildBeforeAnalysisKey);
|
2018-05-07 15:40:14 +02:00
|
|
|
|
2019-09-13 10:49:14 +02:00
|
|
|
return map;
|
2018-05-07 15:40:14 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-25 15:46:15 +02:00
|
|
|
ClangDiagnosticConfigs importDiagnosticConfigsFromCodeModel()
|
|
|
|
|
{
|
2022-05-19 14:48:09 +02:00
|
|
|
const ClangDiagnosticConfigs configs = ClangdSettings::instance().customDiagnosticConfigs();
|
2019-09-25 15:46:15 +02:00
|
|
|
|
|
|
|
|
ClangDiagnosticConfigs tidyClazyConfigs;
|
|
|
|
|
ClangDiagnosticConfigs clangOnlyConfigs;
|
|
|
|
|
std::tie(tidyClazyConfigs, clangOnlyConfigs)
|
|
|
|
|
= Utils::partition(configs, [](const ClangDiagnosticConfig &config) {
|
2023-01-10 15:34:47 +01:00
|
|
|
return !config.checks(ClangToolType::Clazy).isEmpty()
|
|
|
|
|
|| (!config.checks(ClangToolType::Tidy).isEmpty()
|
|
|
|
|
&& config.checks(ClangToolType::Tidy) != "-*");
|
2019-09-25 15:46:15 +02:00
|
|
|
});
|
|
|
|
|
return tidyClazyConfigs;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 10:49:14 +02:00
|
|
|
void ClangToolsSettings::readSettings()
|
2019-08-28 08:56:22 +02:00
|
|
|
{
|
2019-09-25 15:46:15 +02:00
|
|
|
// Transfer tidy/clazy configs from code model
|
|
|
|
|
bool write = false;
|
|
|
|
|
ClangDiagnosticConfigs importedConfigs = importDiagnosticConfigsFromCodeModel();
|
|
|
|
|
m_diagnosticConfigs.append(importedConfigs);
|
|
|
|
|
if (!importedConfigs.isEmpty())
|
|
|
|
|
write = true;
|
|
|
|
|
|
2023-07-14 12:53:50 +02:00
|
|
|
AspectContainer::readSettings();
|
|
|
|
|
|
2023-09-05 09:20:31 +02:00
|
|
|
QtcSettings *s = Core::ICore::settings();
|
2019-09-13 10:49:14 +02:00
|
|
|
s->beginGroup(Constants::SETTINGS_ID);
|
2019-09-25 15:46:15 +02:00
|
|
|
m_diagnosticConfigs.append(diagnosticConfigsFromSettings(s));
|
2019-08-28 08:56:22 +02:00
|
|
|
|
2023-08-25 13:19:26 +02:00
|
|
|
Store map;
|
2019-09-13 10:49:14 +02:00
|
|
|
if (!s->value(oldDiagnosticConfigIdKey).isNull()) {
|
|
|
|
|
map = convertToMapFromVersionBefore410(s);
|
|
|
|
|
write = true;
|
|
|
|
|
} else {
|
2023-08-25 13:19:26 +02:00
|
|
|
Store defaults;
|
2019-10-01 16:53:01 +02:00
|
|
|
defaults.insert(diagnosticConfigIdKey, defaultDiagnosticId().toSetting());
|
2019-09-13 10:49:14 +02:00
|
|
|
defaults.insert(parallelJobsKey, m_runSettings.parallelJobs());
|
2023-04-11 12:49:21 +02:00
|
|
|
defaults.insert(preferConfigFileKey, m_runSettings.preferConfigFile());
|
2019-09-13 10:49:14 +02:00
|
|
|
defaults.insert(buildBeforeAnalysisKey, m_runSettings.buildBeforeAnalysis());
|
2020-07-22 14:52:06 +02:00
|
|
|
defaults.insert(analyzeOpenFilesKey, m_runSettings.analyzeOpenFiles());
|
2019-09-13 10:49:14 +02:00
|
|
|
map = defaults;
|
2023-08-25 13:19:26 +02:00
|
|
|
for (Store::ConstIterator it = defaults.constBegin(); it != defaults.constEnd(); ++it)
|
2019-09-13 10:49:14 +02:00
|
|
|
map.insert(it.key(), s->value(it.key(), it.value()));
|
|
|
|
|
}
|
2019-08-28 08:56:22 +02:00
|
|
|
|
2019-09-13 10:49:14 +02:00
|
|
|
// Run settings
|
|
|
|
|
m_runSettings.fromMap(map);
|
2019-08-28 08:56:22 +02:00
|
|
|
|
2019-09-13 10:49:14 +02:00
|
|
|
s->endGroup();
|
2019-08-28 08:56:22 +02:00
|
|
|
|
2019-09-13 10:49:14 +02:00
|
|
|
if (write)
|
|
|
|
|
writeSettings();
|
2019-08-28 08:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 18:58:18 +02:00
|
|
|
void ClangToolsSettings::writeSettings() const
|
2014-09-25 11:11:58 +02:00
|
|
|
{
|
2023-07-14 12:53:50 +02:00
|
|
|
AspectContainer::writeSettings();
|
|
|
|
|
|
2023-09-05 09:20:31 +02:00
|
|
|
QtcSettings *s = Core::ICore::settings();
|
2019-09-13 10:49:14 +02:00
|
|
|
s->beginGroup(Constants::SETTINGS_ID);
|
2018-05-07 15:40:14 +02:00
|
|
|
|
2019-09-25 15:46:15 +02:00
|
|
|
diagnosticConfigsToSettings(s, m_diagnosticConfigs);
|
2019-08-28 08:56:22 +02:00
|
|
|
|
2023-08-25 13:19:26 +02:00
|
|
|
Store map;
|
2019-09-13 10:49:14 +02:00
|
|
|
m_runSettings.toMap(map);
|
2023-08-25 13:19:26 +02:00
|
|
|
for (Store::ConstIterator it = map.constBegin(); it != map.constEnd(); ++it)
|
2019-09-13 10:49:14 +02:00
|
|
|
s->setValue(it.key(), it.value());
|
2018-05-25 15:30:56 +02:00
|
|
|
|
2019-09-13 10:49:14 +02:00
|
|
|
s->endGroup();
|
2019-09-25 15:46:15 +02:00
|
|
|
|
2023-07-21 18:58:18 +02:00
|
|
|
emit const_cast<ClangToolsSettings *>(this)->changed(); // FIXME: This is the wrong place
|
2014-09-25 11:11:58 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-10 15:51:31 +01:00
|
|
|
FilePath ClangToolsSettings::executable(ClangToolType tool) const
|
2021-06-24 16:13:14 +02:00
|
|
|
{
|
2023-07-14 12:53:50 +02:00
|
|
|
return tool == ClangToolType::Tidy ? clangTidyExecutable() : clazyStandaloneExecutable();
|
2021-06-24 16:13:14 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-10 15:51:31 +01:00
|
|
|
void ClangToolsSettings::setExecutable(ClangToolType tool, const FilePath &path)
|
2021-06-24 16:13:14 +02:00
|
|
|
{
|
2023-01-10 15:51:31 +01:00
|
|
|
if (tool == ClangToolType::Tidy) {
|
2023-07-14 12:53:50 +02:00
|
|
|
clangTidyExecutable.setValue(path);
|
2023-01-10 15:51:31 +01:00
|
|
|
m_clangTidyVersion = {};
|
|
|
|
|
} else {
|
2023-07-14 12:53:50 +02:00
|
|
|
clazyStandaloneExecutable.setValue(path);
|
2023-01-10 15:51:31 +01:00
|
|
|
m_clazyVersion = {};
|
|
|
|
|
}
|
2021-06-24 16:13:14 +02:00
|
|
|
}
|
|
|
|
|
|
2022-08-15 14:28:21 +02:00
|
|
|
static VersionAndSuffix getVersionNumber(VersionAndSuffix &version, const FilePath &toolFilePath)
|
2021-06-24 16:13:14 +02:00
|
|
|
{
|
2022-08-15 14:28:21 +02:00
|
|
|
if (version.first.isNull() && !toolFilePath.isEmpty()) {
|
|
|
|
|
const QString versionString = queryVersion(toolFilePath, QueryFailMode::Silent);
|
|
|
|
|
int suffixIndex = versionString.length() - 1;
|
|
|
|
|
version.first = QVersionNumber::fromString(versionString, &suffixIndex);
|
|
|
|
|
version.second = versionString.mid(suffixIndex);
|
|
|
|
|
}
|
2021-06-24 16:13:14 +02:00
|
|
|
return version;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-15 14:28:21 +02:00
|
|
|
VersionAndSuffix ClangToolsSettings::clangTidyVersion()
|
2021-06-24 16:13:14 +02:00
|
|
|
{
|
2023-01-10 16:36:59 +01:00
|
|
|
return getVersionNumber(instance()->m_clangTidyVersion,
|
|
|
|
|
Internal::toolExecutable(ClangToolType::Tidy));
|
2021-06-24 16:13:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVersionNumber ClangToolsSettings::clazyVersion()
|
|
|
|
|
{
|
2023-01-10 16:36:59 +01:00
|
|
|
return ClazyStandaloneInfo::getInfo(Internal::toolExecutable(ClangToolType::Clazy)).version;
|
2021-06-24 16:13:14 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-25 11:11:58 +02:00
|
|
|
} // namespace Internal
|
2018-03-14 12:58:12 +01:00
|
|
|
} // namespace ClangTools
|