OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / memcheck / suppressiondialog.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator Instrumentation Tools
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Author: Milian Wolff, KDAB (milian.wolff@kdab.com)
8 **
9 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 **
11 **
12 ** GNU Lesser General Public License Usage
13 **
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.
20 **
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.
24 **
25 ** Other Usage
26 **
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.
29 **
30 ** If you have questions regarding the use of this file, please contact
31 ** Nokia at qt-info@nokia.com.
32 **
33 **************************************************************************/
34
35
36 #include "suppressiondialog.h"
37
38 #include "ui_suppressiondialog.h"
39 #include "memcheckerrorview.h"
40 #include "memchecksettings.h"
41
42 #include <projectexplorer/projectexplorer.h>
43 #include <projectexplorer/session.h>
44 #include <projectexplorer/project.h>
45 #include <projectexplorer/projectnodes.h>
46
47 #include <utils/pathchooser.h>
48 #include <utils/qtcassert.h>
49
50 #include <QtCore/QFile>
51 #include <QtGui/QPushButton>
52
53 #include <valgrind/xmlprotocol/suppression.h>
54 #include <valgrind/xmlprotocol/errorlistmodel.h>
55 #include <valgrind/xmlprotocol/stack.h>
56 #include <valgrind/xmlprotocol/frame.h>
57
58 using namespace Analyzer;
59 using namespace Analyzer::Internal;
60 using namespace Valgrind::XmlProtocol;
61
62 namespace {
63 QString suppressionText(const Error &error)
64 {
65     Suppression sup(error.suppression());
66
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));
70     }
71     QTC_ASSERT(sup.frames().size() < 24, qt_noop());
72
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();
78
79         QString newName;
80         if (!frame.functionName().isEmpty())
81             newName = frame.functionName();
82         else if (!frame.object().isEmpty())
83             newName = frame.object();
84
85         if (!newName.isEmpty()) {
86             sup.setName(newName + '[' + sup.kind() + ']');
87         }
88     }
89
90     return sup.toString();
91 }
92
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)
96 {
97     if (error.kind() != suppressed.kind() || error.suppression().isNull())
98         return false;
99
100     const QVector< SuppressionFrame > errorFrames = error.suppression().frames();
101     const QVector< SuppressionFrame > suppressedFrames = suppressed.suppression().frames();
102
103     // limit to 23 frames, see: https://bugs.kde.org/show_bug.cgi?id=255822
104     if (qMin(23, suppressedFrames.size()) > errorFrames.size())
105         return false;
106
107     int frames = 23;
108     if (errorFrames.size() < frames)
109         frames = errorFrames.size();
110
111     if (suppressedFrames.size() < frames)
112         frames = suppressedFrames.size();
113
114     for (int i = 0; i < frames; ++i) {
115         if (errorFrames.at(i) != suppressedFrames.at(i))
116             return false;
117     }
118
119     return true;
120 }
121
122 bool sortIndizesReverse(const QModelIndex &l, const QModelIndex &r)
123 {
124     return l.row() > r.row();
125 }
126
127 }
128
129 SuppressionDialog::SuppressionDialog(MemcheckErrorView *view, QWidget *parent, Qt::WindowFlags f)
130 : QDialog(parent, f),
131   m_view(view),
132   m_ui(new Ui::SuppressionDialog),
133   m_settings(view->settings()),
134   m_cleanupIfCanceled(false)
135 {
136     m_ui->setupUi(this);
137
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;
144         }
145     }
146
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()),
153             SLOT(validate()));
154     connect(m_ui->suppressionEdit->document(), SIGNAL(contentsChanged()),
155             SLOT(validate()));
156
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();
162     }
163     foreach(const QModelIndex &index, indizes) {
164         Error error = m_view->model()->data(index, ErrorListModel::ErrorRole).value<Error>();
165         if (!error.suppression().isNull())
166             m_errors << error;
167     }
168
169     foreach(const Error &error, m_errors)
170         suppressions += suppressionText(error);
171
172     m_ui->suppressionEdit->setPlainText(suppressions);
173
174     setWindowTitle(tr("Save Suppression"));
175 }
176
177 bool SuppressionDialog::shouldShow() const
178 {
179     return !m_errors.isEmpty();
180 }
181
182 void SuppressionDialog::accept()
183 {
184     const QString path = m_ui->fileChooser->path();
185     QTC_ASSERT(!path.isEmpty(), return);
186     QTC_ASSERT(!m_ui->suppressionEdit->toPlainText().trimmed().isEmpty(), return);
187
188     QFile file(path);
189     bool opened = file.open(QIODevice::WriteOnly | QIODevice::Append);
190     QTC_ASSERT(opened, return);
191
192     QTextStream stream(&file);
193     stream << m_ui->suppressionEdit->toPlainText();
194     file.close();
195
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);
202                 break;
203             }
204         }
205     }
206
207     m_settings->subConfig<AbstractMemcheckSettings>()->addSuppressionFiles(QStringList(path));
208
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());
214         Q_UNUSED(removed);
215     }
216
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>();
221
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());
226                 Q_UNUSED(removed);
227                 // gets increased in the for loop again
228                 --row;
229                 break;
230             }
231         }
232     }
233
234     // select a new item
235     m_view->setCurrentIndex(indizes.first());
236
237     QDialog::accept();
238 }
239
240 void SuppressionDialog::reject()
241 {
242     if (m_cleanupIfCanceled) {
243         QFile::remove(m_view->defaultSuppressionFile());
244     }
245
246     QDialog::reject();
247 }
248
249 void SuppressionDialog::validate()
250 {
251     bool valid = m_ui->fileChooser->isValid() &&
252                     !m_ui->suppressionEdit->toPlainText().trimmed().isEmpty();
253
254     m_ui->buttonBox->button(QDialogButtonBox::Save)->setEnabled(valid);
255 }