2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2011-03-04 12:15:19 +01:00
|
|
|
|
|
|
|
|
#include "suppressiondialog.h"
|
2011-04-04 14:39:29 +02:00
|
|
|
|
2011-03-04 12:15:19 +01:00
|
|
|
#include "memcheckerrorview.h"
|
2011-07-12 16:47:32 +02:00
|
|
|
#include "valgrindsettings.h"
|
2022-07-08 16:08:27 +02:00
|
|
|
#include "valgrindtr.h"
|
2011-03-04 12:15:19 +01:00
|
|
|
|
2011-07-13 14:58:54 +02:00
|
|
|
#include "xmlprotocol/suppression.h"
|
|
|
|
|
#include "xmlprotocol/errorlistmodel.h"
|
|
|
|
|
#include "xmlprotocol/stack.h"
|
|
|
|
|
#include "xmlprotocol/frame.h"
|
|
|
|
|
|
2011-03-04 12:15:19 +01:00
|
|
|
#include <projectexplorer/projectexplorer.h>
|
|
|
|
|
#include <projectexplorer/session.h>
|
|
|
|
|
#include <projectexplorer/project.h>
|
|
|
|
|
#include <projectexplorer/projectnodes.h>
|
|
|
|
|
|
2014-06-16 18:25:52 +04:00
|
|
|
#include <utils/algorithm.h>
|
2011-03-04 12:15:19 +01:00
|
|
|
#include <utils/pathchooser.h>
|
2011-03-30 15:15:15 +02:00
|
|
|
#include <utils/fileutils.h>
|
2011-03-04 12:15:19 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDialogButtonBox>
|
2021-03-09 17:40:29 +01:00
|
|
|
#include <QFile>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QFormLayout>
|
|
|
|
|
#include <QLabel>
|
|
|
|
|
#include <QPlainTextEdit>
|
2021-03-09 17:40:29 +01:00
|
|
|
#include <QPushButton>
|
2011-03-04 12:15:19 +01:00
|
|
|
|
2021-09-30 17:12:10 +02:00
|
|
|
using namespace Utils;
|
2011-03-04 12:15:19 +01:00
|
|
|
using namespace Valgrind::XmlProtocol;
|
|
|
|
|
|
2011-07-13 14:58:54 +02:00
|
|
|
namespace Valgrind {
|
|
|
|
|
namespace Internal {
|
2011-05-18 17:05:49 +02:00
|
|
|
|
2011-07-13 14:58:54 +02:00
|
|
|
static QString suppressionText(const Error &error)
|
2011-03-04 12:15:19 +01:00
|
|
|
{
|
|
|
|
|
Suppression sup(error.suppression());
|
|
|
|
|
|
|
|
|
|
// workaround: https://bugs.kde.org/show_bug.cgi?id=255822
|
2011-07-13 14:58:54 +02:00
|
|
|
if (sup.frames().size() >= 24)
|
2011-03-04 12:15:19 +01:00
|
|
|
sup.setFrames(sup.frames().mid(0, 23));
|
2021-09-30 17:12:10 +02:00
|
|
|
QTC_CHECK(sup.frames().size() < 24);
|
2011-03-04 12:15:19 +01:00
|
|
|
|
|
|
|
|
// try to set some useful name automatically, instead of "insert_name_here"
|
|
|
|
|
// we take the last stack frame and append the suppression kind, e.g.:
|
|
|
|
|
// QDebug::operator<<(bool) [Memcheck:Cond]
|
2019-01-17 01:54:13 +01:00
|
|
|
if (!error.stacks().isEmpty() && !error.stacks().constFirst().frames().isEmpty()) {
|
|
|
|
|
const Frame frame = error.stacks().constFirst().frames().constFirst();
|
2011-03-04 12:15:19 +01:00
|
|
|
|
|
|
|
|
QString newName;
|
|
|
|
|
if (!frame.functionName().isEmpty())
|
|
|
|
|
newName = frame.functionName();
|
|
|
|
|
else if (!frame.object().isEmpty())
|
|
|
|
|
newName = frame.object();
|
|
|
|
|
|
2011-07-13 14:58:54 +02:00
|
|
|
if (!newName.isEmpty())
|
2018-12-01 20:56:21 +02:00
|
|
|
sup.setName(newName + '[' + sup.kind() + ']');
|
2011-03-04 12:15:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sup.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// @p error input error, which might get hidden when it has the same stack
|
|
|
|
|
/// @p suppressed the error that got suppressed already
|
2011-05-18 17:05:49 +02:00
|
|
|
static bool equalSuppression(const Error &error, const Error &suppressed)
|
2011-03-04 12:15:19 +01:00
|
|
|
{
|
|
|
|
|
if (error.kind() != suppressed.kind() || error.suppression().isNull())
|
|
|
|
|
return false;
|
|
|
|
|
|
2011-05-18 17:05:49 +02:00
|
|
|
const SuppressionFrames errorFrames = error.suppression().frames();
|
|
|
|
|
const SuppressionFrames suppressedFrames = suppressed.suppression().frames();
|
2011-03-04 12:15:19 +01:00
|
|
|
|
|
|
|
|
// limit to 23 frames, see: https://bugs.kde.org/show_bug.cgi?id=255822
|
|
|
|
|
if (qMin(23, suppressedFrames.size()) > errorFrames.size())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
int frames = 23;
|
|
|
|
|
if (errorFrames.size() < frames)
|
|
|
|
|
frames = errorFrames.size();
|
|
|
|
|
|
|
|
|
|
if (suppressedFrames.size() < frames)
|
|
|
|
|
frames = suppressedFrames.size();
|
|
|
|
|
|
2011-07-13 14:58:54 +02:00
|
|
|
for (int i = 0; i < frames; ++i)
|
2011-03-04 12:15:19 +01:00
|
|
|
if (errorFrames.at(i) != suppressedFrames.at(i))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-16 16:06:25 +01:00
|
|
|
SuppressionDialog::SuppressionDialog(MemcheckErrorView *view, const QList<Error> &errors)
|
|
|
|
|
: m_view(view),
|
2011-05-18 17:05:49 +02:00
|
|
|
m_settings(view->settings()),
|
2011-12-16 16:06:25 +01:00
|
|
|
m_cleanupIfCanceled(false),
|
|
|
|
|
m_errors(errors),
|
2021-09-30 17:12:10 +02:00
|
|
|
m_fileChooser(new PathChooser(this)),
|
2011-12-16 16:06:25 +01:00
|
|
|
m_suppressionEdit(new QPlainTextEdit(this))
|
2011-03-04 12:15:19 +01:00
|
|
|
{
|
2022-07-08 16:08:27 +02:00
|
|
|
setWindowTitle(Tr::tr("Save Suppression"));
|
2011-12-16 16:06:25 +01:00
|
|
|
|
2022-07-08 16:08:27 +02:00
|
|
|
auto fileLabel = new QLabel(Tr::tr("Suppression File:"), this);
|
2011-12-16 16:06:25 +01:00
|
|
|
|
2022-07-08 16:08:27 +02:00
|
|
|
auto suppressionsLabel = new QLabel(Tr::tr("Suppression:"), this);
|
2011-12-16 16:06:25 +01:00
|
|
|
suppressionsLabel->setBuddy(m_suppressionEdit);
|
|
|
|
|
|
|
|
|
|
QFont font;
|
2018-12-01 20:56:21 +02:00
|
|
|
font.setFamily("Monospace");
|
2011-12-16 16:06:25 +01:00
|
|
|
m_suppressionEdit->setFont(font);
|
|
|
|
|
|
|
|
|
|
m_buttonBox = new QDialogButtonBox(this);
|
|
|
|
|
m_buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Save);
|
|
|
|
|
|
2018-12-10 08:11:18 +01:00
|
|
|
auto formLayout = new QFormLayout(this);
|
2011-12-16 16:06:25 +01:00
|
|
|
formLayout->addRow(fileLabel, m_fileChooser);
|
|
|
|
|
formLayout->addRow(suppressionsLabel);
|
|
|
|
|
formLayout->addRow(m_suppressionEdit);
|
|
|
|
|
formLayout->addRow(m_buttonBox);
|
2011-03-04 12:15:19 +01:00
|
|
|
|
2021-09-30 17:12:10 +02:00
|
|
|
const FilePath defaultSuppFile = view->defaultSuppressionFile();
|
|
|
|
|
if (!defaultSuppFile.exists() && defaultSuppFile.ensureExistingFile())
|
|
|
|
|
m_cleanupIfCanceled = true;
|
2011-03-04 12:15:19 +01:00
|
|
|
|
2021-09-30 17:12:10 +02:00
|
|
|
m_fileChooser->setExpectedKind(PathChooser::File);
|
2018-12-01 20:56:21 +02:00
|
|
|
m_fileChooser->setHistoryCompleter("Valgrind.Suppression.History");
|
2011-12-16 16:06:25 +01:00
|
|
|
m_fileChooser->setPath(defaultSuppFile.fileName());
|
2018-12-01 20:56:21 +02:00
|
|
|
m_fileChooser->setPromptDialogFilter("*.supp");
|
2022-07-08 16:08:27 +02:00
|
|
|
m_fileChooser->setPromptDialogTitle(Tr::tr("Select Suppression File"));
|
2011-03-04 12:15:19 +01:00
|
|
|
|
|
|
|
|
QString suppressions;
|
2022-10-07 14:46:06 +02:00
|
|
|
for (const Error &error : std::as_const(m_errors))
|
2011-03-04 12:15:19 +01:00
|
|
|
suppressions += suppressionText(error);
|
|
|
|
|
|
2011-12-16 16:06:25 +01:00
|
|
|
m_suppressionEdit->setPlainText(suppressions);
|
2011-03-04 12:15:19 +01:00
|
|
|
|
2021-09-30 17:12:10 +02:00
|
|
|
connect(m_fileChooser, &PathChooser::validChanged,
|
2015-02-06 12:39:07 +02:00
|
|
|
this, &SuppressionDialog::validate);
|
|
|
|
|
connect(m_suppressionEdit->document(), &QTextDocument::contentsChanged,
|
|
|
|
|
this, &SuppressionDialog::validate);
|
|
|
|
|
connect(m_buttonBox, &QDialogButtonBox::accepted,
|
|
|
|
|
this, &SuppressionDialog::accept);
|
|
|
|
|
connect(m_buttonBox, &QDialogButtonBox::rejected,
|
|
|
|
|
this, &SuppressionDialog::reject);
|
2011-03-04 12:15:19 +01:00
|
|
|
}
|
|
|
|
|
|
2011-12-16 16:06:25 +01:00
|
|
|
void SuppressionDialog::maybeShow(MemcheckErrorView *view)
|
2011-10-28 13:22:45 +02:00
|
|
|
{
|
2011-12-16 16:06:25 +01:00
|
|
|
QModelIndexList indices = view->selectionModel()->selectedRows();
|
|
|
|
|
// Can happen when using arrow keys to navigate and shortcut to trigger suppression:
|
|
|
|
|
if (indices.isEmpty() && view->selectionModel()->currentIndex().isValid())
|
|
|
|
|
indices.append(view->selectionModel()->currentIndex());
|
2011-10-28 13:22:45 +02:00
|
|
|
|
2011-12-16 16:06:25 +01:00
|
|
|
QList<XmlProtocol::Error> errors;
|
2022-10-07 14:46:06 +02:00
|
|
|
for (const QModelIndex &index : std::as_const(indices)) {
|
2011-12-16 16:06:25 +01:00
|
|
|
Error error = view->model()->data(index, ErrorListModel::ErrorRole).value<Error>();
|
|
|
|
|
if (!error.suppression().isNull())
|
|
|
|
|
errors.append(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (errors.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
SuppressionDialog dialog(view, errors);
|
|
|
|
|
dialog.exec();
|
2011-03-04 16:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
2011-03-04 12:15:19 +01:00
|
|
|
void SuppressionDialog::accept()
|
|
|
|
|
{
|
2021-09-30 17:12:10 +02:00
|
|
|
const FilePath path = m_fileChooser->filePath();
|
2011-03-04 12:15:19 +01:00
|
|
|
QTC_ASSERT(!path.isEmpty(), return);
|
2011-12-16 16:06:25 +01:00
|
|
|
QTC_ASSERT(!m_suppressionEdit->toPlainText().trimmed().isEmpty(), return);
|
2011-03-04 12:15:19 +01:00
|
|
|
|
2021-09-30 17:12:10 +02:00
|
|
|
FileSaver saver(path, QIODevice::Append);
|
2013-06-04 18:02:49 +02:00
|
|
|
if (!saver.hasError()) {
|
|
|
|
|
QTextStream stream(saver.file());
|
|
|
|
|
stream << m_suppressionEdit->toPlainText();
|
|
|
|
|
saver.setResult(&stream);
|
|
|
|
|
}
|
2011-03-30 15:15:15 +02:00
|
|
|
if (!saver.finalize(this))
|
|
|
|
|
return;
|
2011-03-04 12:15:19 +01:00
|
|
|
|
2011-12-16 16:06:25 +01:00
|
|
|
// Add file to project if there is a project containing this file on the file system.
|
2021-05-18 09:47:07 +02:00
|
|
|
if (!ProjectExplorer::SessionManager::projectForFile(path)) {
|
2017-03-01 17:53:15 +01:00
|
|
|
for (ProjectExplorer::Project *p : ProjectExplorer::SessionManager::projects()) {
|
2014-05-02 12:53:36 +02:00
|
|
|
if (path.startsWith(p->projectDirectory().toString())) {
|
2021-07-14 16:49:42 +02:00
|
|
|
p->rootProjectNode()->addFiles({path});
|
2011-03-04 12:15:19 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 17:12:10 +02:00
|
|
|
m_settings->suppressions.addSuppressionFile(path);
|
2011-03-04 12:15:19 +01:00
|
|
|
|
2011-07-13 14:58:54 +02:00
|
|
|
QModelIndexList indices = m_view->selectionModel()->selectedRows();
|
2014-06-16 18:25:52 +04:00
|
|
|
Utils::sort(indices, [](const QModelIndex &l, const QModelIndex &r) {
|
|
|
|
|
return l.row() > r.row();
|
|
|
|
|
});
|
2011-12-16 16:06:25 +01:00
|
|
|
QAbstractItemModel *model = m_view->model();
|
2022-10-07 14:46:06 +02:00
|
|
|
for (const QModelIndex &index : std::as_const(indices)) {
|
2011-12-16 16:06:25 +01:00
|
|
|
bool removed = model->removeRow(index.row());
|
2011-03-04 12:15:19 +01:00
|
|
|
QTC_ASSERT(removed, qt_noop());
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(removed)
|
2011-03-04 12:15:19 +01:00
|
|
|
}
|
|
|
|
|
|
2011-12-16 16:06:25 +01:00
|
|
|
// One suppression might hide multiple rows, care for that.
|
|
|
|
|
for (int row = 0; row < model->rowCount(); ++row ) {
|
|
|
|
|
const Error rowError = model->data(
|
|
|
|
|
model->index(row, 0), ErrorListModel::ErrorRole).value<Error>();
|
2011-03-04 12:15:19 +01:00
|
|
|
|
2022-10-07 14:46:06 +02:00
|
|
|
for (const Error &error : std::as_const(m_errors)) {
|
2011-03-04 12:15:19 +01:00
|
|
|
if (equalSuppression(rowError, error)) {
|
2011-12-16 16:06:25 +01:00
|
|
|
bool removed = model->removeRow(row);
|
|
|
|
|
QTC_CHECK(removed);
|
|
|
|
|
// Gets incremented in the for loop again.
|
2011-03-04 12:15:19 +01:00
|
|
|
--row;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-16 16:06:25 +01:00
|
|
|
// Select a new item.
|
2011-07-13 14:58:54 +02:00
|
|
|
m_view->setCurrentIndex(indices.first());
|
2011-03-04 12:15:19 +01:00
|
|
|
|
|
|
|
|
QDialog::accept();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SuppressionDialog::reject()
|
|
|
|
|
{
|
2011-05-18 17:05:49 +02:00
|
|
|
if (m_cleanupIfCanceled)
|
2021-09-30 17:12:10 +02:00
|
|
|
m_view->defaultSuppressionFile().removeFile();
|
2011-03-04 12:15:19 +01:00
|
|
|
|
|
|
|
|
QDialog::reject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SuppressionDialog::validate()
|
|
|
|
|
{
|
2011-12-16 16:06:25 +01:00
|
|
|
bool valid = m_fileChooser->isValid()
|
|
|
|
|
&& !m_suppressionEdit->toPlainText().trimmed().isEmpty();
|
2011-03-04 12:15:19 +01:00
|
|
|
|
2011-12-16 16:06:25 +01:00
|
|
|
m_buttonBox->button(QDialogButtonBox::Save)->setEnabled(valid);
|
2011-03-04 12:15:19 +01:00
|
|
|
}
|
2011-05-18 17:05:49 +02:00
|
|
|
|
|
|
|
|
} // namespace Internal
|
2011-05-23 13:50:28 +02:00
|
|
|
} // namespace Valgrind
|