Commit 6e796591 authored by Christian Kandeler's avatar Christian Kandeler

Allow users to suppress diagnostics.

This patch deals with what is likely the most common use case:
Filtering specific messages at a particular location. The current
granularity is essentially per-file (and per-function, where possible),
which seems more useful than taking line numbers into
account, as that would not be robust with regards to code changes elsewhere
in the file. We can fine-tune this if the need arises.

Change-Id: I4e9b2671fa199339cc3b995953d072b840cd3205
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@theqtcompany.com>
parent a13818f5
......@@ -12,6 +12,9 @@ SOURCES += \
clangstaticanalyzerlogfilereader.cpp \
clangstaticanalyzerpathchooser.cpp \
clangstaticanalyzerplugin.cpp \
clangstaticanalyzerprojectsettings.cpp \
clangstaticanalyzerprojectsettingsmanager.cpp \
clangstaticanalyzerprojectsettingswidget.cpp \
clangstaticanalyzerruncontrol.cpp \
clangstaticanalyzerruncontrolfactory.cpp \
clangstaticanalyzerrunner.cpp \
......@@ -29,6 +32,9 @@ HEADERS += \
clangstaticanalyzerlogfilereader.h \
clangstaticanalyzerpathchooser.h \
clangstaticanalyzerplugin.h \
clangstaticanalyzerprojectsettings.h \
clangstaticanalyzerprojectsettingsmanager.h \
clangstaticanalyzerprojectsettingswidget.h \
clangstaticanalyzerruncontrolfactory.h \
clangstaticanalyzerruncontrol.h \
clangstaticanalyzerrunner.h \
......@@ -37,7 +43,8 @@ HEADERS += \
clangstaticanalyzerutils.h
FORMS += \
clangstaticanalyzerconfigwidget.ui
clangstaticanalyzerconfigwidget.ui \
clangstaticanalyzerprojectsettingswidget.ui
equals(TEST, 1) {
HEADERS += clangstaticanalyzerunittests.h
......
......@@ -32,6 +32,13 @@ QtcPlugin {
"clangstaticanalyzerpathchooser.h",
"clangstaticanalyzerplugin.cpp",
"clangstaticanalyzerplugin.h",
"clangstaticanalyzerprojectsettings.cpp",
"clangstaticanalyzerprojectsettings.h",
"clangstaticanalyzerprojectsettingsmanager.cpp",
"clangstaticanalyzerprojectsettingsmanager.h",
"clangstaticanalyzerprojectsettingswidget.cpp",
"clangstaticanalyzerprojectsettingswidget.h",
"clangstaticanalyzerprojectsettingswidget.ui",
"clangstaticanalyzerruncontrol.cpp",
"clangstaticanalyzerruncontrol.h",
"clangstaticanalyzerruncontrolfactory.cpp",
......
......@@ -18,9 +18,15 @@
#include "clangstaticanalyzerdiagnosticmodel.h"
#include "clangstaticanalyzerprojectsettingsmanager.h"
#include "clangstaticanalyzerutils.h"
#include <projectexplorer/project.h>
#include <projectexplorer/session.h>
#include <utils/qtcassert.h>
#include <QCoreApplication>
#include <QFileInfo>
namespace ClangStaticAnalyzer {
namespace Internal {
......@@ -118,5 +124,70 @@ QVariant ClangStaticAnalyzerDiagnosticModel::data(const QModelIndex &index, int
return QVariant();
}
ClangStaticAnalyzerDiagnosticFilterModel::ClangStaticAnalyzerDiagnosticFilterModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
// So that when a user closes and re-opens a project and *then* clicks "Suppress",
// we enter that information into the project settings.
connect(ProjectExplorer::SessionManager::instance(),
&ProjectExplorer::SessionManager::projectAdded, this,
[this](ProjectExplorer::Project *project) {
if (!m_project && project->projectDirectory() == m_lastProjectDirectory)
setProject(project);
});
}
void ClangStaticAnalyzerDiagnosticFilterModel::setProject(ProjectExplorer::Project *project)
{
QTC_ASSERT(project, return);
if (m_project) {
disconnect(ProjectSettingsManager::getSettings(m_project),
&ProjectSettings::suppressedDiagnosticsChanged, this,
&ClangStaticAnalyzerDiagnosticFilterModel::handleSuppressedDiagnosticsChanged);
}
m_project = project;
m_lastProjectDirectory = m_project->projectDirectory();
connect(ProjectSettingsManager::getSettings(m_project),
&ProjectSettings::suppressedDiagnosticsChanged,
this, &ClangStaticAnalyzerDiagnosticFilterModel::handleSuppressedDiagnosticsChanged);
handleSuppressedDiagnosticsChanged();
}
void ClangStaticAnalyzerDiagnosticFilterModel::addSuppressedDiagnostic(
const SuppressedDiagnostic &diag)
{
QTC_ASSERT(!m_project, return);
m_suppressedDiagnostics << diag;
invalidate();
}
bool ClangStaticAnalyzerDiagnosticFilterModel::filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const
{
Q_UNUSED(sourceParent);
const Diagnostic diag = static_cast<ClangStaticAnalyzerDiagnosticModel *>(sourceModel())
->diagnostics().at(sourceRow);
foreach (const SuppressedDiagnostic &d, m_suppressedDiagnostics) {
if (d.description != diag.description)
continue;
QString filePath = d.filePath.toString();
QFileInfo fi(filePath);
if (fi.isRelative())
filePath = m_lastProjectDirectory.toString() + QLatin1Char('/') + filePath;
if (filePath == diag.location.filePath)
return false;
}
return true;
}
void ClangStaticAnalyzerDiagnosticFilterModel::handleSuppressedDiagnosticsChanged()
{
QTC_ASSERT(m_project, return);
m_suppressedDiagnostics
= ProjectSettingsManager::getSettings(m_project)->suppressedDiagnostics();
invalidate();
}
} // namespace Internal
} // namespace ClangStaticAnalyzer
......@@ -20,8 +20,15 @@
#define CLANGSTATICANALYZERDIAGNOSTICMODEL_H
#include "clangstaticanalyzerlogfilereader.h"
#include "clangstaticanalyzerprojectsettings.h"
#include <utils/fileutils.h>
#include <QAbstractListModel>
#include <QPointer>
#include <QSortFilterProxyModel>
namespace ProjectExplorer { class Project; }
namespace ClangStaticAnalyzer {
namespace Internal {
......@@ -45,6 +52,26 @@ private:
QList<Diagnostic> m_diagnostics;
};
class ClangStaticAnalyzerDiagnosticFilterModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
ClangStaticAnalyzerDiagnosticFilterModel(QObject *parent = 0);
void setProject(ProjectExplorer::Project *project);
void addSuppressedDiagnostic(const SuppressedDiagnostic &diag);
ProjectExplorer::Project *project() const { return m_project; }
private:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
void handleSuppressedDiagnosticsChanged();
QPointer<ProjectExplorer::Project> m_project;
Utils::FileName m_lastProjectDirectory;
SuppressedDiagnosticsList m_suppressedDiagnostics;
};
} // namespace Internal
} // namespace ClangStaticAnalyzer
......
......@@ -19,10 +19,15 @@
#include "clangstaticanalyzerdiagnosticview.h"
#include "clangstaticanalyzerlogfilereader.h"
#include "clangstaticanalyzerdiagnosticmodel.h"
#include "clangstaticanalyzerprojectsettings.h"
#include "clangstaticanalyzerprojectsettingsmanager.h"
#include "clangstaticanalyzerutils.h"
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
#include <QAction>
#include <QCoreApplication>
#include <QDebug>
#include <QFileInfo>
......@@ -196,13 +201,18 @@ DetailedErrorDelegate::SummaryLineInfo ClangStaticAnalyzerDiagnosticDelegate::su
return info;
}
Diagnostic ClangStaticAnalyzerDiagnosticDelegate::getDiagnostic(const QModelIndex &index) const
{
return index.data(Qt::UserRole).value<Diagnostic>();
}
QWidget *ClangStaticAnalyzerDiagnosticDelegate::createDetailsWidget(const QFont &font,
const QModelIndex &index,
QWidget *parent) const
{
QWidget *widget = new QWidget(parent);
const Diagnostic diagnostic = index.data(Qt::UserRole).value<Diagnostic>();
const Diagnostic diagnostic = getDiagnostic(index);
if (!diagnostic.isValid())
return widget;
......@@ -240,7 +250,7 @@ QString ClangStaticAnalyzerDiagnosticDelegate::textualRepresentation() const
{
QTC_ASSERT(m_detailsIndex.isValid(), return QString());
const Diagnostic diagnostic = m_detailsIndex.data(Qt::UserRole).value<Diagnostic>();
const Diagnostic diagnostic = getDiagnostic(m_detailsIndex);
QTC_ASSERT(diagnostic.isValid(), return QString());
// Create summary
......@@ -268,6 +278,39 @@ ClangStaticAnalyzerDiagnosticView::ClangStaticAnalyzerDiagnosticView(QWidget *pa
ClangStaticAnalyzerDiagnosticDelegate *delegate
= new ClangStaticAnalyzerDiagnosticDelegate(this);
setItemDelegate(delegate);
m_suppressAction = new QAction(tr("Suppress this diagnostic"), this);
connect(m_suppressAction, &QAction::triggered, [this](bool) { suppressCurrentDiagnostic(); });
}
void ClangStaticAnalyzerDiagnosticView::suppressCurrentDiagnostic()
{
const QModelIndexList indexes = selectedIndexes();
QTC_ASSERT(indexes.count() == 1, return);
const Diagnostic diag = static_cast<ClangStaticAnalyzerDiagnosticDelegate *>(itemDelegate())
->getDiagnostic(indexes.first());
QTC_ASSERT(diag.isValid(), return);
// If the original project was closed, we work directly on the filter model, otherwise
// we go via the project settings.
auto * const filterModel = static_cast<ClangStaticAnalyzerDiagnosticFilterModel *>(model());
ProjectExplorer::Project * const project = filterModel->project();
if (project) {
Utils::FileName filePath = Utils::FileName::fromString(diag.location.filePath);
const Utils::FileName relativeFilePath
= filePath.relativeChildPath(project->projectDirectory());
if (!relativeFilePath.isEmpty())
filePath = relativeFilePath;
const SuppressedDiagnostic supDiag(filePath, diag.description, diag.issueContextKind,
diag.issueContext, diag.explainingSteps.count());
ProjectSettingsManager::getSettings(project)->addSuppressedDiagnostic(supDiag);
} else {
filterModel->addSuppressedDiagnostic(SuppressedDiagnostic(diag));
}
}
QList<QAction *> ClangStaticAnalyzerDiagnosticView::customActions() const
{
return QList<QAction *>() << m_suppressAction;
}
} // namespace Internal
......
......@@ -23,6 +23,7 @@
namespace ClangStaticAnalyzer {
namespace Internal {
class Diagnostic;
class ClangStaticAnalyzerDiagnosticView : public Analyzer::DetailedErrorView
{
......@@ -30,6 +31,13 @@ class ClangStaticAnalyzerDiagnosticView : public Analyzer::DetailedErrorView
public:
ClangStaticAnalyzerDiagnosticView(QWidget *parent = 0);
private:
void suppressCurrentDiagnostic();
QList<QAction *> customActions() const;
QAction *m_suppressAction;
};
class ClangStaticAnalyzerDiagnosticDelegate : public Analyzer::DetailedErrorDelegate
......@@ -38,6 +46,7 @@ public:
ClangStaticAnalyzerDiagnosticDelegate(QListView *parent);
SummaryLineInfo summaryInfo(const QModelIndex &index) const;
Diagnostic getDiagnostic(const QModelIndex &index) const;
private:
QWidget *createDetailsWidget(const QFont &font, const QModelIndex &index,
......
......@@ -19,6 +19,7 @@
#include "clangstaticanalyzerplugin.h"
#include "clangstaticanalyzerconfigwidget.h"
#include "clangstaticanalyzerprojectsettingswidget.h"
#include "clangstaticanalyzerruncontrolfactory.h"
#include "clangstaticanalyzertool.h"
......@@ -35,6 +36,7 @@
#include <coreplugin/coreconstants.h>
#include <coreplugin/dialogs/ioptionspage.h>
#include <licensechecker/licensecheckerplugin.h>
#include <projectexplorer/projectpanelfactory.h>
#include <extensionsystem/pluginmanager.h>
......@@ -106,6 +108,12 @@ bool ClangStaticAnalyzerPlugin::initialize(const QStringList &arguments, QString
// In the initialize method, a plugin can be sure that the plugins it
// depends on have initialized their members.
auto panelFactory = new ProjectExplorer::ProjectPanelFactory();
panelFactory->setPriority(100);
panelFactory->setDisplayName(tr("Clang Static Analyzer Settings"));
panelFactory->setSimpleCreateWidgetFunction<ProjectSettingsWidget>(QIcon());
ProjectExplorer::ProjectPanelFactory::registerFactory(panelFactory);
LicenseChecker::LicenseCheckerPlugin *licenseChecker
= ExtensionSystem::PluginManager::getObject<LicenseChecker::LicenseCheckerPlugin>();
......
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd
** All rights reserved.
** For any questions to The Qt Company, please use contact form at http://www.qt.io/contact-us
**
** This file is part of the Qt Enterprise Qt Quick Profiler Add-on.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company.
**
** If you have questions regarding the use of this file, please use
** contact form at http://www.qt.io/contact-us
**
****************************************************************************/
#include "clangstaticanalyzerprojectsettings.h"
#include "clangstaticanalyzerdiagnostic.h"
#include <utils/qtcassert.h>
namespace ClangStaticAnalyzer {
namespace Internal {
static QString suppressedDiagnosticsKey()
{
return QLatin1String("ClangStaticAnalyzer.SuppressedDiagnostics");
}
static QString suppressedDiagnosticFilePathKey()
{
return QLatin1String("ClangStaticAnalyzer.SuppressedDiagnosticFilePath");
}
static QString suppressedDiagnosticMessageKey()
{
return QLatin1String("ClangStaticAnalyzer.SuppressedDiagnosticMessage");
}
static QString suppressedDiagnosticContextKindKey()
{
return QLatin1String("ClangStaticAnalyzer.SuppressedDiagnosticContextKind");
}
static QString suppressedDiagnosticContextKey()
{
return QLatin1String("ClangStaticAnalyzer.SuppressedDiagnosticContext");
}
static QString suppressedDiagnosticUniquifierKey()
{
return QLatin1String("ClangStaticAnalyzer.SuppressedDiagnosticUniquifier");
}
ProjectSettings::ProjectSettings(ProjectExplorer::Project *project) : m_project(project)
{
load();
connect(project, &ProjectExplorer::Project::aboutToSaveSettings, this,
&ProjectSettings::store);
}
void ProjectSettings::addSuppressedDiagnostic(const SuppressedDiagnostic &diag)
{
QTC_ASSERT(!m_suppressedDiagnostics.contains(diag), return);
m_suppressedDiagnostics << diag;
emit suppressedDiagnosticsChanged();
}
void ProjectSettings::removeSuppressedDiagnostic(const SuppressedDiagnostic &diag)
{
const bool wasPresent = m_suppressedDiagnostics.removeOne(diag);
QTC_ASSERT(wasPresent, return);
emit suppressedDiagnosticsChanged();
}
void ProjectSettings::removeAllSuppressedDiagnostics()
{
m_suppressedDiagnostics.clear();
emit suppressedDiagnosticsChanged();
}
void ProjectSettings::load()
{
const QVariantList list = m_project->namedSettings(suppressedDiagnosticsKey()).toList();
foreach (const QVariant &v, list) {
const QVariantMap diag = v.toMap();
const QString fp = diag.value(suppressedDiagnosticFilePathKey()).toString();
if (fp.isEmpty())
continue;
const QString message = diag.value(suppressedDiagnosticMessageKey()).toString();
if (message.isEmpty())
continue;
Utils::FileName fullPath = Utils::FileName::fromString(fp);
if (fullPath.toFileInfo().isRelative()) {
fullPath = m_project->projectDirectory();
fullPath.appendPath(fp);
}
if (!fullPath.exists())
continue;
const QString contextKind = diag.value(suppressedDiagnosticContextKindKey()).toString();
const QString context = diag.value(suppressedDiagnosticContextKey()).toString();
const int uniquifier = diag.value(suppressedDiagnosticUniquifierKey()).toInt();
m_suppressedDiagnostics << SuppressedDiagnostic(Utils::FileName::fromString(fp), message,
contextKind, context, uniquifier);
}
emit suppressedDiagnosticsChanged();
}
void ProjectSettings::store()
{
QVariantList list;
foreach (const SuppressedDiagnostic &diag, m_suppressedDiagnostics) {
QVariantMap diagMap;
diagMap.insert(suppressedDiagnosticFilePathKey(), diag.filePath.toString());
diagMap.insert(suppressedDiagnosticMessageKey(), diag.description);
diagMap.insert(suppressedDiagnosticContextKindKey(), diag.contextKind);
diagMap.insert(suppressedDiagnosticContextKey(), diag.context);
diagMap.insert(suppressedDiagnosticUniquifierKey(), diag.uniquifier);
list << diagMap;
}
m_project->setNamedSettings(suppressedDiagnosticsKey(), list);
}
SuppressedDiagnostic::SuppressedDiagnostic(const Diagnostic &diag)
: filePath(Utils::FileName::fromString(diag.location.filePath))
, description(diag.description)
, contextKind(diag.issueContextKind)
, context(diag.issueContext)
, uniquifier(diag.explainingSteps.count())
{
}
} // namespace Internal
} // namespace ClangStaticAnalyzer
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd
** All rights reserved.
** For any questions to The Qt Company, please use contact form at http://www.qt.io/contact-us
**
** This file is part of the Qt Enterprise Qt Quick Profiler Add-on.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company.
**
** If you have questions regarding the use of this file, please use
** contact form at http://www.qt.io/contact-us
**
****************************************************************************/
#ifndef CLANGSTATICANALYZERPROJECTSETTINGS_H
#define CLANGSTATICANALYZERPROJECTSETTINGS_H
#include <projectexplorer/project.h>
#include <utils/fileutils.h>
#include <QList>
#include <QObject>
namespace ClangStaticAnalyzer {
namespace Internal {
class Diagnostic;
class SuppressedDiagnostic
{
public:
SuppressedDiagnostic(const Utils::FileName &filePath, const QString &description,
const QString &contextKind, const QString &context, int uniquifier)
: filePath(filePath)
, description(description)
, contextKind(contextKind)
, context(context)
, uniquifier(uniquifier)
{
}
SuppressedDiagnostic(const Diagnostic &diag);
Utils::FileName filePath; // Relative for files in project, absolute otherwise.
QString description;
QString contextKind;
QString context;
int uniquifier;
};
inline bool operator==(const SuppressedDiagnostic &d1, const SuppressedDiagnostic &d2)
{
return d1.filePath == d2.filePath && d1.description == d2.description
&& d1.contextKind == d2.contextKind && d1.context == d2.context
&& d1.uniquifier == d2.uniquifier;
}
typedef QList<SuppressedDiagnostic> SuppressedDiagnosticsList;
class ProjectSettings : public QObject
{
Q_OBJECT
public:
ProjectSettings(ProjectExplorer::Project *project);
SuppressedDiagnosticsList suppressedDiagnostics() const { return m_suppressedDiagnostics; }
void addSuppressedDiagnostic(const SuppressedDiagnostic &diag);
void removeSuppressedDiagnostic(const SuppressedDiagnostic &diag);
void removeAllSuppressedDiagnostics();
signals:
void suppressedDiagnosticsChanged();
private:
void load();
void store();
ProjectExplorer::Project * const m_project;
SuppressedDiagnosticsList m_suppressedDiagnostics;
};
} // namespace Internal
} // namespace ClangStaticAnalyzer
#endif // Include guard.
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd
** All rights reserved.
** For any questions to The Qt Company, please use contact form at http://www.qt.io/contact-us
**
** This file is part of the Qt Enterprise Qt Quick Profiler Add-on.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company.
**
** If you have questions regarding the use of this file, please use
** contact form at http://www.qt.io/contact-us
**
****************************************************************************/
#include "clangstaticanalyzerprojectsettingsmanager.h"
#include "clangstaticanalyzerprojectsettings.h"
#include <projectexplorer/session.h>
namespace ClangStaticAnalyzer {
namespace Internal {
ProjectSettingsManager::ProjectSettingsManager()
{
QObject::connect(ProjectExplorer::SessionManager::instance(),
&ProjectExplorer::SessionManager::aboutToRemoveProject,
&ProjectSettingsManager::handleProjectToBeRemoved);
}
ProjectSettings *ProjectSettingsManager::getSettings(ProjectExplorer::Project *project)
{
auto &settings = m_settings[project];
if (!settings)
settings.reset(new ProjectSettings(project));
return settings.data();
}
void ProjectSettingsManager::handleProjectToBeRemoved(ProjectExplorer::Project *project)
{
m_settings.remove(project);
}
ProjectSettingsManager::SettingsMap ProjectSettingsManager::m_settings;
} // namespace Internal
} // namespace ClangStaticAnalyzer
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd
** All rights reserved.
** For any questions to The Qt Company, please use contact form at http://www.qt.io/contact-us
**
** This file is part of the Qt Enterprise Qt Quick Profiler Add-on.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company.
**
** If you have questions regarding the use of this file, please use
** contact form at http://www.qt.io/contact-us
**
****************************************************************************/
#ifndef CLANGSTATICANALYZERPROJECTSETTINGSMANAGER_H
#define CLANGSTATICANALYZERPROJECTSETTINGSMANAGER_H