1 /**************************************************************************
3 ** This file is part of Qt Creator Instrumentation Tools
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Author: Milian Wolff, KDAB (milian.wolff@kdab.com)
9 ** Contact: Nokia Corporation (info@qt.nokia.com)
12 ** GNU Lesser General Public License Usage
14 ** This file may be used under the terms of the GNU Lesser General Public
15 ** License version 2.1 as published by the Free Software Foundation and
16 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
17 ** Please review the following information to ensure the GNU Lesser General
18 ** Public License version 2.1 requirements will be met:
19 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
21 ** In addition, as a special exception, Nokia gives you certain additional
22 ** rights. These rights are described in the Nokia Qt LGPL Exception
23 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 ** Alternatively, this file may be used in accordance with the terms and
28 ** conditions contained in a signed written agreement between you and Nokia.
30 ** If you have questions regarding the use of this file, please contact
31 ** Nokia at qt-info@nokia.com.
33 **************************************************************************/
36 #include "suppressiondialog.h"
38 #include "ui_suppressiondialog.h"
39 #include "memcheckerrorview.h"
40 #include "memchecksettings.h"
42 #include <projectexplorer/projectexplorer.h>
43 #include <projectexplorer/session.h>
44 #include <projectexplorer/project.h>
45 #include <projectexplorer/projectnodes.h>
47 #include <utils/pathchooser.h>
48 #include <utils/qtcassert.h>
50 #include <QtCore/QFile>
51 #include <QtGui/QPushButton>
53 #include <valgrind/xmlprotocol/suppression.h>
54 #include <valgrind/xmlprotocol/errorlistmodel.h>
55 #include <valgrind/xmlprotocol/stack.h>
56 #include <valgrind/xmlprotocol/frame.h>
58 using namespace Analyzer;
59 using namespace Analyzer::Internal;
60 using namespace Valgrind::XmlProtocol;
63 QString suppressionText(const Error &error)
65 Suppression sup(error.suppression());
67 // workaround: https://bugs.kde.org/show_bug.cgi?id=255822
68 if (sup.frames().size() >= 24) {
69 sup.setFrames(sup.frames().mid(0, 23));
71 QTC_ASSERT(sup.frames().size() < 24, qt_noop());
73 // try to set some useful name automatically, instead of "insert_name_here"
74 // we take the last stack frame and append the suppression kind, e.g.:
75 // QDebug::operator<<(bool) [Memcheck:Cond]
76 if (!error.stacks().isEmpty() && !error.stacks().first().frames().isEmpty()) {
77 const Frame &frame = error.stacks().first().frames().first();
80 if (!frame.functionName().isEmpty())
81 newName = frame.functionName();
82 else if (!frame.object().isEmpty())
83 newName = frame.object();
85 if (!newName.isEmpty()) {
86 sup.setName(newName + '[' + sup.kind() + ']');
90 return sup.toString();
93 /// @p error input error, which might get hidden when it has the same stack
94 /// @p suppressed the error that got suppressed already
95 static inline bool equalSuppression(const Error &error, const Error &suppressed)
97 if (error.kind() != suppressed.kind() || error.suppression().isNull())
100 const QVector< SuppressionFrame > errorFrames = error.suppression().frames();
101 const QVector< SuppressionFrame > suppressedFrames = suppressed.suppression().frames();
103 // limit to 23 frames, see: https://bugs.kde.org/show_bug.cgi?id=255822
104 if (qMin(23, suppressedFrames.size()) > errorFrames.size())
108 if (errorFrames.size() < frames)
109 frames = errorFrames.size();
111 if (suppressedFrames.size() < frames)
112 frames = suppressedFrames.size();
114 for (int i = 0; i < frames; ++i) {
115 if (errorFrames.at(i) != suppressedFrames.at(i))
122 bool sortIndizesReverse(const QModelIndex &l, const QModelIndex &r)
124 return l.row() > r.row();
129 SuppressionDialog::SuppressionDialog(MemcheckErrorView *view, QWidget *parent, Qt::WindowFlags f)
130 : QDialog(parent, f),
132 m_ui(new Ui::SuppressionDialog),
133 m_settings(view->settings()),
134 m_cleanupIfCanceled(false)
138 ///NOTE: pathchooser requires existing files...
139 QFile defaultSuppFile(view->defaultSuppressionFile());
140 if (!defaultSuppFile.exists()) {
141 if (defaultSuppFile.open(QIODevice::WriteOnly)) {
142 defaultSuppFile.close();
143 m_cleanupIfCanceled = true;
147 //NOTE: first set kind, then set path since otherwise the file will be seen as "invalid"
148 m_ui->fileChooser->setExpectedKind(Utils::PathChooser::File);
149 m_ui->fileChooser->setPath(defaultSuppFile.fileName());
150 m_ui->fileChooser->setPromptDialogFilter(QLatin1String("*.supp"));
151 m_ui->fileChooser->setPromptDialogTitle(tr("Select Suppression File"));
152 connect(m_ui->fileChooser, SIGNAL(validChanged()),
154 connect(m_ui->suppressionEdit->document(), SIGNAL(contentsChanged()),
157 QString suppressions;
158 QModelIndexList indizes = m_view->selectionModel()->selectedRows();
159 if (indizes.isEmpty() && m_view->selectionModel()->currentIndex().isValid()) {
160 // can happen when using arrow keys to navigate and shortcut to trigger suppression
161 indizes << m_view->selectionModel()->currentIndex();
163 foreach(const QModelIndex &index, indizes) {
164 Error error = m_view->model()->data(index, ErrorListModel::ErrorRole).value<Error>();
165 if (!error.suppression().isNull())
169 foreach(const Error &error, m_errors)
170 suppressions += suppressionText(error);
172 m_ui->suppressionEdit->setPlainText(suppressions);
174 setWindowTitle(tr("Save Suppression"));
177 bool SuppressionDialog::shouldShow() const
179 return !m_errors.isEmpty();
182 void SuppressionDialog::accept()
184 const QString path = m_ui->fileChooser->path();
185 QTC_ASSERT(!path.isEmpty(), return);
186 QTC_ASSERT(!m_ui->suppressionEdit->toPlainText().trimmed().isEmpty(), return);
189 bool opened = file.open(QIODevice::WriteOnly | QIODevice::Append);
190 QTC_ASSERT(opened, return);
192 QTextStream stream(&file);
193 stream << m_ui->suppressionEdit->toPlainText();
196 // add file to project (if there is a project that contains this file on the file system)
197 ProjectExplorer::SessionManager *session = ProjectExplorer::ProjectExplorerPlugin::instance()->session();
198 if (!session->projectForFile(path)) {
199 foreach(ProjectExplorer::Project *p, session->projects()) {
200 if (path.startsWith(p->projectDirectory())) {
201 p->rootProjectNode()->addFiles(ProjectExplorer::UnknownFileType, QStringList() << path);
207 m_settings->subConfig<AbstractMemcheckSettings>()->addSuppressionFiles(QStringList(path));
209 QModelIndexList indizes = m_view->selectionModel()->selectedRows();
210 qSort(indizes.begin(), indizes.end(), sortIndizesReverse);
211 foreach(const QModelIndex &index, indizes) {
212 bool removed = m_view->model()->removeRow(index.row());
213 QTC_ASSERT(removed, qt_noop());
217 // one suppression might hide multiple rows, care for that
218 for (int row = 0; row < m_view->model()->rowCount(); ++row ) {
219 const Error rowError = m_view->model()->data(
220 m_view->model()->index(row, 0), ErrorListModel::ErrorRole).value<Error>();
222 foreach(const Error &error, m_errors) {
223 if (equalSuppression(rowError, error)) {
224 bool removed = m_view->model()->removeRow(row);
225 QTC_ASSERT(removed, qt_noop());
227 // gets increased in the for loop again
235 m_view->setCurrentIndex(indizes.first());
240 void SuppressionDialog::reject()
242 if (m_cleanupIfCanceled) {
243 QFile::remove(m_view->defaultSuppressionFile());
249 void SuppressionDialog::validate()
251 bool valid = m_ui->fileChooser->isValid() &&
252 !m_ui->suppressionEdit->toPlainText().trimmed().isEmpty();
254 m_ui->buttonBox->button(QDialogButtonBox::Save)->setEnabled(valid);