CppCheck: Use new settings pattern

Change-Id: Ia2de8e684690ab7bfbdcf7ba417df1f2174433de
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2023-07-17 08:44:12 +02:00
parent bce9b9ffaa
commit 8e7ac2e59d
5 changed files with 64 additions and 41 deletions

View File

@@ -16,6 +16,7 @@
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/variablechooser.h> #include <utils/variablechooser.h>
#include <coreplugin/dialogs/ioptionspage.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <debugger/analyzer/analyzericons.h> #include <debugger/analyzer/analyzericons.h>
@@ -25,14 +26,16 @@ using namespace Utils;
namespace Cppcheck::Internal { namespace Cppcheck::Internal {
CppcheckOptions::CppcheckOptions() CppcheckSettings &settings()
{
static CppcheckSettings theSettings;
return theSettings;
}
CppcheckSettings::CppcheckSettings()
{ {
setId(Constants::OPTIONS_PAGE_ID);
setDisplayName(Tr::tr("Cppcheck"));
setCategory("T.Analyzer");
setDisplayCategory(::Debugger::Tr::tr("Analyzer"));
setCategoryIconPath(Analyzer::Icons::SETTINGSCATEGORY_ANALYZER);
setSettingsGroup("Cppcheck"); setSettingsGroup("Cppcheck");
setAutoApply(false);
binary.setSettingsKey("binary"); binary.setSettingsKey("binary");
binary.setExpectedKind(PathChooser::ExistingCommand); binary.setExpectedKind(PathChooser::ExistingCommand);
@@ -108,7 +111,7 @@ CppcheckOptions::CppcheckOptions()
readSettings(); readSettings();
} }
std::function<Layouting::LayoutItem()> CppcheckOptions::layouter() std::function<Layouting::LayoutItem()> CppcheckSettings::layouter()
{ {
return [this] { return [this] {
using namespace Layouting; using namespace Layouting;
@@ -136,4 +139,20 @@ std::function<Layouting::LayoutItem()> CppcheckOptions::layouter()
}; };
} }
class CppCheckSettingsPage final : public Core::IOptionsPage
{
public:
CppCheckSettingsPage()
{
setId(Constants::OPTIONS_PAGE_ID);
setDisplayName(Tr::tr("Cppcheck"));
setCategory("T.Analyzer");
setDisplayCategory(::Debugger::Tr::tr("Analyzer"));
setCategoryIconPath(Analyzer::Icons::SETTINGSCATEGORY_ANALYZER);
setSettingsProvider([] { return &settings(); });
}
};
const CppCheckSettingsPage settingsPage;
} // Cppcheck::Internal } // Cppcheck::Internal

View File

@@ -3,14 +3,16 @@
#pragma once #pragma once
#include <coreplugin/dialogs/ioptionspage.h> #include <utils/aspects.h>
#include <functional>
namespace Cppcheck::Internal { namespace Cppcheck::Internal {
class CppcheckOptions final : public Core::PagedSettings class CppcheckSettings final : public Utils::AspectContainer
{ {
public: public:
CppcheckOptions(); CppcheckSettings();
std::function<Layouting::LayoutItem()> layouter(); std::function<Layouting::LayoutItem()> layouter();
@@ -32,4 +34,7 @@ public:
Utils::BoolAspect guessArguments{this}; Utils::BoolAspect guessArguments{this};
}; };
CppcheckSettings &settings();
} // Cppcheck::Internal } // Cppcheck::Internal

View File

@@ -5,6 +5,7 @@
#include "cppcheckconstants.h" #include "cppcheckconstants.h"
#include "cppcheckdiagnosticview.h" #include "cppcheckdiagnosticview.h"
#include "cppcheckoptions.h"
#include "cppchecktextmarkmanager.h" #include "cppchecktextmarkmanager.h"
#include "cppchecktool.h" #include "cppchecktool.h"
#include "cppchecktr.h" #include "cppchecktr.h"
@@ -39,11 +40,10 @@ public:
explicit CppcheckPluginPrivate(); explicit CppcheckPluginPrivate();
CppcheckTextMarkManager marks; CppcheckTextMarkManager marks;
CppcheckOptions options; CppcheckTool tool{marks, Constants::CHECK_PROGRESS_ID};
CppcheckTool tool{options, marks, Constants::CHECK_PROGRESS_ID};
CppcheckTrigger trigger{marks, tool}; CppcheckTrigger trigger{marks, tool};
DiagnosticsModel manualRunModel; DiagnosticsModel manualRunModel;
CppcheckTool manualRunTool{options, manualRunModel, Constants::MANUAL_CHECK_PROGRESS_ID}; CppcheckTool manualRunTool{manualRunModel, Constants::MANUAL_CHECK_PROGRESS_ID};
Utils::Perspective perspective{Constants::PERSPECTIVE_ID, ::Cppcheck::Tr::tr("Cppcheck")}; Utils::Perspective perspective{Constants::PERSPECTIVE_ID, ::Cppcheck::Tr::tr("Cppcheck")};
QAction *manualRunAction; QAction *manualRunAction;
@@ -55,7 +55,7 @@ public:
CppcheckPluginPrivate::CppcheckPluginPrivate() CppcheckPluginPrivate::CppcheckPluginPrivate()
{ {
tool.updateOptions(); tool.updateOptions();
connect(&options, &AspectContainer::changed, [this] { connect(&settings(), &AspectContainer::changed, [this] {
tool.updateOptions(); tool.updateOptions();
trigger.recheck(); trigger.recheck();
}); });
@@ -112,7 +112,7 @@ void CppcheckPluginPrivate::startManualRun()
manualRunTool.updateOptions(); manualRunTool.updateOptions();
auto optionsWidget = options.layouter()().emerge(); auto optionsWidget = settings().layouter()().emerge();
ManualRunDialog dialog(optionsWidget, project); ManualRunDialog dialog(optionsWidget, project);
if (dialog.exec() == ManualRunDialog::Rejected) if (dialog.exec() == ManualRunDialog::Rejected)

View File

@@ -26,8 +26,7 @@ using namespace Utils;
namespace Cppcheck::Internal { namespace Cppcheck::Internal {
CppcheckTool::CppcheckTool(CppcheckOptions &options, CppcheckDiagnosticManager &manager, const Id &progressId) : CppcheckTool::CppcheckTool(CppcheckDiagnosticManager &manager, const Id &progressId) :
m_options(options),
m_manager(manager), m_manager(manager),
m_progressRegexp("^.* checked (\\d+)% done$"), m_progressRegexp("^.* checked (\\d+)% done$"),
m_messageRegexp("^(.+),(\\d+),(\\w+),(\\w+),(.*)$"), m_messageRegexp("^(.+),(\\d+),(\\w+),(\\w+),(.*)$"),
@@ -43,7 +42,7 @@ CppcheckTool::~CppcheckTool() = default;
void CppcheckTool::updateOptions() void CppcheckTool::updateOptions()
{ {
m_filters.clear(); m_filters.clear();
for (const QString &pattern : m_options.ignoredPatterns().split(',')) { for (const QString &pattern : settings().ignoredPatterns().split(',')) {
const QString trimmedPattern = pattern.trimmed(); const QString trimmedPattern = pattern.trimmed();
if (trimmedPattern.isEmpty()) if (trimmedPattern.isEmpty())
continue; continue;
@@ -69,45 +68,47 @@ void CppcheckTool::updateArguments()
m_cachedAdditionalArguments.clear(); m_cachedAdditionalArguments.clear();
CppcheckSettings &s = settings();
QStringList arguments; QStringList arguments;
if (!m_options.customArguments().isEmpty()) { if (!s.customArguments().isEmpty()) {
Utils::MacroExpander *expander = Utils::globalMacroExpander(); Utils::MacroExpander *expander = Utils::globalMacroExpander();
const QString expanded = expander->expand(m_options.customArguments()); const QString expanded = expander->expand(s.customArguments());
arguments.push_back(expanded); arguments.push_back(expanded);
} }
if (m_options.warning()) if (s.warning())
arguments.push_back("--enable=warning"); arguments.push_back("--enable=warning");
if (m_options.style()) if (s.style())
arguments.push_back("--enable=style"); arguments.push_back("--enable=style");
if (m_options.performance()) if (s.performance())
arguments.push_back("--enable=performance"); arguments.push_back("--enable=performance");
if (m_options.portability()) if (s.portability())
arguments.push_back("--enable=portability"); arguments.push_back("--enable=portability");
if (m_options.information()) if (s.information())
arguments.push_back("--enable=information"); arguments.push_back("--enable=information");
if (m_options.unusedFunction()) if (s.unusedFunction())
arguments.push_back("--enable=unusedFunction"); arguments.push_back("--enable=unusedFunction");
if (m_options.missingInclude()) if (s.missingInclude())
arguments.push_back("--enable=missingInclude"); arguments.push_back("--enable=missingInclude");
if (m_options.inconclusive()) if (s.inconclusive())
arguments.push_back("--inconclusive"); arguments.push_back("--inconclusive");
if (m_options.forceDefines()) if (s.forceDefines())
arguments.push_back("--force"); arguments.push_back("--force");
if (!m_options.unusedFunction() && !m_options.customArguments().contains("-j ")) if (!s.unusedFunction() && !s.customArguments().contains("-j "))
arguments.push_back("-j " + QString::number(QThread::idealThreadCount())); arguments.push_back("-j " + QString::number(QThread::idealThreadCount()));
arguments.push_back("--template=\"{file},{line},{severity},{id},{message}\""); arguments.push_back("--template=\"{file},{line},{severity},{id},{message}\"");
m_runner->reconfigure(m_options.binary(), arguments.join(' ')); m_runner->reconfigure(s.binary(), arguments.join(' '));
} }
QStringList CppcheckTool::additionalArguments(const CppEditor::ProjectPart &part) const QStringList CppcheckTool::additionalArguments(const CppEditor::ProjectPart &part) const
{ {
QStringList result; QStringList result;
if (m_options.addIncludePaths()) { if (settings().addIncludePaths()) {
for (const ProjectExplorer::HeaderPath &path : part.headerPaths) { for (const ProjectExplorer::HeaderPath &path : part.headerPaths) {
const QString projectDir = m_project->projectDirectory().toString(); const QString projectDir = m_project->projectDirectory().toString();
if (path.type == ProjectExplorer::HeaderPathType::User if (path.type == ProjectExplorer::HeaderPathType::User
@@ -116,7 +117,7 @@ QStringList CppcheckTool::additionalArguments(const CppEditor::ProjectPart &part
} }
} }
if (!m_options.guessArguments()) if (!settings().guessArguments())
return result; return result;
using Version = Utils::LanguageVersion; using Version = Utils::LanguageVersion;
@@ -221,7 +222,7 @@ void CppcheckTool::stop(const Utils::FilePaths &files)
void CppcheckTool::startParsing() void CppcheckTool::startParsing()
{ {
if (m_options.showOutput()) { if (settings().showOutput()) {
const QString message = Tr::tr("Cppcheck started: \"%1\".").arg(m_runner->currentCommand()); const QString message = Tr::tr("Cppcheck started: \"%1\".").arg(m_runner->currentCommand());
Core::MessageManager::writeSilently(message); Core::MessageManager::writeSilently(message);
} }
@@ -240,7 +241,7 @@ void CppcheckTool::parseOutputLine(const QString &line)
if (line.isEmpty()) if (line.isEmpty())
return; return;
if (m_options.showOutput()) if (settings().showOutput())
Core::MessageManager::writeSilently(line); Core::MessageManager::writeSilently(line);
enum Matches { Percentage = 1 }; enum Matches { Percentage = 1 };
@@ -271,7 +272,7 @@ void CppcheckTool::parseErrorLine(const QString &line)
if (line.isEmpty()) if (line.isEmpty())
return; return;
if (m_options.showOutput()) if (settings().showOutput())
Core::MessageManager::writeSilently(line); Core::MessageManager::writeSilently(line);
enum Matches { File = 1, Line, Severity, Id, Message }; enum Matches { File = 1, Line, Severity, Id, Message };
@@ -296,7 +297,7 @@ void CppcheckTool::parseErrorLine(const QString &line)
void CppcheckTool::finishParsing() void CppcheckTool::finishParsing()
{ {
if (m_options.showOutput()) if (settings().showOutput())
Core::MessageManager::writeSilently(Tr::tr("Cppcheck finished.")); Core::MessageManager::writeSilently(Tr::tr("Cppcheck finished."));
QTC_ASSERT(m_progress, return); QTC_ASSERT(m_progress, return);

View File

@@ -3,7 +3,7 @@
#pragma once #pragma once
#include <cppcheck/cppcheckoptions.h> #include <utils/id.h>
#include <QFutureInterface> #include <QFutureInterface>
#include <QPointer> #include <QPointer>
@@ -24,14 +24,13 @@ namespace Cppcheck::Internal {
class CppcheckRunner; class CppcheckRunner;
class CppcheckDiagnosticManager; class CppcheckDiagnosticManager;
class CppcheckOptions;
class CppcheckTool final : public QObject class CppcheckTool final : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
CppcheckTool(CppcheckOptions &options, CppcheckDiagnosticManager &manager, const Utils::Id &progressId); CppcheckTool(CppcheckDiagnosticManager &manager, const Utils::Id &progressId);
~CppcheckTool() override; ~CppcheckTool() override;
void updateOptions(); void updateOptions();
@@ -49,7 +48,6 @@ private:
void addToQueue(const Utils::FilePaths &files, const CppEditor::ProjectPart &part); void addToQueue(const Utils::FilePaths &files, const CppEditor::ProjectPart &part);
QStringList additionalArguments(const CppEditor::ProjectPart &part) const; QStringList additionalArguments(const CppEditor::ProjectPart &part) const;
CppcheckOptions &m_options;
CppcheckDiagnosticManager &m_manager; CppcheckDiagnosticManager &m_manager;
QPointer<ProjectExplorer::Project> m_project; QPointer<ProjectExplorer::Project> m_project;
std::unique_ptr<CppcheckRunner> m_runner; std::unique_ptr<CppcheckRunner> m_runner;