-
Notifications
You must be signed in to change notification settings - Fork 2
/
warningmodel.cpp
164 lines (136 loc) · 5.2 KB
/
warningmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
This file is part of warning-viewer.
Copyright (C) 2015 Sergio Martins <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, permission is given to link this program
with any edition of Qt, and distribute the resulting executable,
without including the source code for Qt in the source distribution.
*/
#include "warningmodel.h"
#include "warning.h"
#include <QFile>
#include <QRegularExpression>
#include <QDebug>
WarningModel::WarningModel(QObject *parent)
: QAbstractTableModel(parent)
{
connect(this, &WarningModel::rowsInserted, this, &WarningModel::countChanged);
connect(this, &WarningModel::rowsRemoved, this, &WarningModel::countChanged);
connect(this, &WarningModel::modelReset, this, &WarningModel::countChanged);
connect(this, &WarningModel::layoutChanged, this, &WarningModel::countChanged);
}
WarningModel::~WarningModel()
{
}
int WarningModel::columnCount(const QModelIndex &) const
{
return ColumnCount;
}
int WarningModel::rowCount(const QModelIndex & /*parent*/) const
{
return m_warnings.count();
}
QVariant WarningModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= m_warnings.count())
return QVariant();
const int col = index.column();
const Warning &warning = m_warnings.at(index.row());
if (role == Qt::DisplayRole) {
if (col == ShortFilenameColumn) {
return warning.shortFileName() + QStringLiteral(":%1:%2").arg(warning.lineNumber()).arg(warning.columnNumber());
} else if (col == TextColumn) {
return warning.m_text;
} else if (col == WarningNameColumn) {
return warning.m_warningName;
}
} else if (role == Qt::ToolTipRole) {
return warning.m_completeText;
} else if (role == WarningNameRole) {
return warning.m_warningName;
} else if (role == WarningRole) {
return QVariant::fromValue<Warning>(warning);
}
return QVariant();
}
QVariant WarningModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation != Qt::Horizontal)
return {};
if (role == Qt::DisplayRole) {
switch (section) {
case ShortFilenameColumn:
return tr("file name");
case TextColumn:
return "description";
case WarningNameColumn:
return "warning";
}
}
return QAbstractTableModel::headerData(section, orientation, role);
}
int WarningModel::count() const
{
return m_warnings.count();
}
bool WarningModel::loadFile(const QString &filename)
{
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
emit loadFinished(false, tr("Couldn't open file %1").arg(filename));
return false;
}
beginResetModel();
QRegularExpression re("(.*)warning:(.*)\\[(.*)\\].*");
QSet<QByteArray> uniqueWarnings;
QHash<QString, Warning> uniqueWarningsHash;
while (!file.atEnd()) {
uniqueWarnings.insert(file.readLine());
}
foreach (const QByteArray &warning, uniqueWarnings) {
const QString warningStr = QString::fromUtf8(warning);
QRegularExpressionMatch match = re.match(warningStr);
if (match.hasMatch()) {
Warning warn;
warn.m_completeText = match.captured(0);
const QStringList filenameTokens = match.captured(1).trimmed().split(':');
warn.setFilename(filenameTokens.first());
// filenameTokens is for example { "file.cpp", 10, 1 }
if (filenameTokens.size() > 1) {
bool ok = false;
int line = filenameTokens[1].toInt(&ok);
if (ok) {
warn.setLineNumber(line);
if (filenameTokens.size() > 2) {
int column = filenameTokens[2].toInt(&ok);
if (ok)
warn.setColumnNumber(column);
}
}
}
if (warn.filename().isEmpty())
continue;
warn.m_text = match.captured(2);
warn.m_warningName = match.captured(3).replace("-W", "");
uniqueWarningsHash.insert(warn.toString(), warn);
}
}
m_warnings.clear();
m_warnings.reserve(uniqueWarningsHash.size());
for (const auto &warning : uniqueWarningsHash)
m_warnings.push_back(warning);
endResetModel();
qDebug() << "Loaded " << m_warnings.size() << " unfiltered warnings from" << filename;
emit loadFinished(true, QString());
return true;
}