forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresultsform.cpp
More file actions
353 lines (306 loc) · 11 KB
/
resultsform.cpp
File metadata and controls
353 lines (306 loc) · 11 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "resultsform.h"
#include "applicationsettings.h"
#include "resultsmodel.h"
#include "ui_resultsform.h"
#include <QDate>
#include <QDebug>
#include <QDir>
#include <QMenu>
#include <QMessageBox>
#include <QProcess>
#include <QTextCodec>
static QString lastResultFile(const QString &projectName)
{
QString ret;
ApplicationSettings settings;
QDir dir(settings.resultsFolder);
dir.setSorting(QDir::Name);
dir.setNameFilters(QStringList() << "*.xml");
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
foreach(const QFileInfo fileinfo, dir.entryInfoList()) {
const QString filename = fileinfo.canonicalFilePath();
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
continue;
QDomDocument doc;
if (!doc.setContent(&file))
continue;
const QDomElement rootElement = doc.documentElement();
if (rootElement.tagName() != "results")
continue;
const QDomElement metaElement = rootElement.firstChildElement("meta");
if (metaElement.isNull())
continue;
const QDomElement projectElement = metaElement.firstChildElement("project");
if (projectElement.text() == projectName)
ret = filename;
}
return ret;
}
ResultsForm::ResultsForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::ResultsForm)
{
ui->setupUi(this);
resultsmodel = new ResultsModel(this);
ui->results->setModel(resultsmodel);
currentScan.analyser = 0;
currentScan.filenum = 0;
currentScan.files.clear();
currentScan.process = 0;
currentScan.project = ProjectList::Project();
}
ResultsForm::~ResultsForm()
{
resultsmodel->saveIfModified();
delete ui;
}
QStringList ResultsForm::getresults() const
{
QStringList ret;
const QString projectName = resultsmodel->getProjectName();
if (projectName.isEmpty())
return ret;
ApplicationSettings settings;
QDir dir(settings.resultsFolder);
dir.setSorting(QDir::Name);
dir.setNameFilters(QStringList() << "*.xml");
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
foreach(const QFileInfo fileinfo, dir.entryInfoList()) {
const QString filename = fileinfo.canonicalFilePath();
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
continue;
QDomDocument doc;
if (!doc.setContent(&file))
continue;
const QDomElement rootElement = doc.documentElement();
if (rootElement.tagName() != "results")
continue;
const QDomElement metaElement = rootElement.firstChildElement("meta");
if (metaElement.isNull())
continue;
const QDomElement projectElement = metaElement.firstChildElement("project");
if (projectElement.text() == projectName)
ret.append(filename);
}
ret.sort();
return ret;
}
void ResultsForm::contextMenu(QPoint pos)
{
const int row = ui->results->currentIndex().row();
QMenu contextMenu(tr("Context menu"),0);
QAction *hideId = new QAction(tr("Hide id"), &contextMenu);
contextMenu.addAction(hideId);
QAction *hideAllOtherId = new QAction(tr("Hide all other id"), &contextMenu);
contextMenu.addAction(hideAllOtherId);
QAction *showAll = new QAction(tr("Show all"), &contextMenu);
contextMenu.addAction(showAll);
QMenu diffMenu(tr("Diff against"),0);
contextMenu.addMenu(&diffMenu);
QList<QAction*> diff;
foreach(const QString filename, getresults()) {
QAction *action = new QAction(filename, &diffMenu);
diffMenu.addAction(action);
diff.append(action);
}
const QAction *action = contextMenu.exec(ui->results->viewport()->mapToGlobal(pos));
if (action==hideId)
resultsmodel->hideId(row);
else if (action==hideAllOtherId)
resultsmodel->hideAllOtherId(row);
else if (action==showAll)
resultsmodel->showAll();
else if (diff.contains(const_cast<QAction*>(action))) {
resultsmodel->diffAgainstFile(action->text());
}
}
static QStringList filelist(const QString &path)
{
QStringList files;
QDir dir(path);
dir.setSorting(QDir::Name);
dir.setNameFilters(QStringList() << "*");
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
foreach(const QString dirname, dir.entryList()) {
files += filelist(path + "/" + dirname);
}
dir.setNameFilters(QStringList() << "*.cpp" << "*.c");
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
foreach(const QString file, dir.entryList()) {
files << path + "/" + file;
}
return files;
}
void ResultsForm::scan(const ProjectList::Project &project)
{
qDebug() << "ResultsForm::scan()";
if (currentScan.process)
return;
ui->progressBar->setVisible(true);
ui->triageWidget->setVisible(false);
currentScan.process = new QProcess(this);
currentScan.project = project;
connect(currentScan.process, SIGNAL(readyReadStandardError()), this, SLOT(scanAddResult()));
connect(currentScan.process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(scanFinished()));
resultsmodel->saveIfModified();
resultsmodel->clear();
ui->progressBar->setVisible(true);
ui->progressBar->setEnabled(true);
ui->progressBar->setValue(0);
currentScan.files = filelist(project.path);
currentScan.filenum = 0;
currentScan.analyser = 0;
scanFinished();
}
void ResultsForm::scanAddResult()
{
const QString errout = QTextCodec::codecForMib(106)->toUnicode(currentScan.process->readAllStandardError());
if (errout.isEmpty())
return;
const QStringList errorlist(errout.split(QRegExp("[\r\n]")));
// Get project path (use / separator, end with /)
QString path = currentScan.project.path;
path.replace('\\','/');
if (!path.endsWith('/'))
path += '/';
// Add results..
foreach(const QString errmsg, errorlist) {
// Add error message
resultsmodel->addresult(path,errmsg);
}
}
void ResultsForm::scanFinished()
{
ApplicationSettings settings;
const QString commands[] = { QString(),
settings.clang,
settings.cppcheck,
settings.gcc
};
const bool enabled[] = { false,
currentScan.project.clang.compiler,
currentScan.project.cppcheck.enabled,
currentScan.project.gcc.enabled
};
// Scanning of a file was finished. Scan next file..
QString cmd;
while (cmd.isEmpty() && currentScan.filenum < currentScan.files.size()) {
currentScan.analyser = (currentScan.analyser + 1) & 3;
if (currentScan.analyser == 0)
++currentScan.filenum;
if (!enabled[currentScan.analyser])
continue;
cmd = commands[currentScan.analyser];
if (cmd.indexOf("/")>=0 && !QFileInfo(cmd).exists())
cmd.clear();
}
QStringList args;
if (cmd == settings.gcc) {
args << "-fsyntax-only";
args << "--std=c++11";
args << "-Wall";
args << "-Wextra";
args << "-pedantic";
args << "-Wabi";
args << "-Wcast-qual";
args << "-Wconversion";
args << "-Wfloat-equal";
args << "-Winline";
args << "-Wlogical-op";
args << "-Wmissing-declarations";
args << "-Wmissing-format-attribute";
args << "-Wno-long-long";
args << "-Woverloaded-virtual";
args << "-Wpacked";
args << "-Wredundant-decls";
args << "-Wshadow";
args << "-Wsign-conversion";
args << "-Wsign-promo";
args << "-Wunreachable-code";
args << "-Wno-sign-compare";
} else if (cmd == settings.clang) {
args << "-fsyntax-only";
args << "--std=c++11";
args << "-Weverything";
} else if (cmd == settings.cppcheck) {
args << "--template={file}:{line}:{severity}:{message} [{id}]" << "--enable=style" << "--inconclusive";
}
if (currentScan.filenum >= currentScan.files.size()) {
ui->progressBar->setValue(0);
ui->progressBar->setEnabled(false);
// Save results to file so they can be viewed later
const QString last = lastResultFile(currentScan.project.name);
QString filename;
for (int i = 1; i < 10000; i++) {
filename = settings.resultsFolder + "/"
+ QDate::currentDate().toString("yyyy-MM-dd-")
+ QString().sprintf("%i",i)
+ ".xml";
if (!QFileInfo(filename).exists() || filename == last)
break;
}
if (!resultsmodel->save(filename, currentScan.project.name))
QMessageBox::warning(this, QObject::tr("Results"), QObject::tr("Failed to save results"));
// Clear "currentScan"
currentScan.process = NULL;
currentScan.analyser = 0;
currentScan.filenum = 0;
currentScan.files.clear();
currentScan.project = ProjectList::Project();
} else {
QString dir = cmd;
dir = dir.replace('\\','/');
if (dir.contains('/')) {
dir.truncate(dir.lastIndexOf('/'));
currentScan.process->setWorkingDirectory(dir);
QDir::setCurrent(dir);
}
currentScan.process->start(cmd, args << currentScan.files[currentScan.filenum]);
ui->progressBar->setValue(100 * currentScan.filenum / currentScan.files.size());
}
}
void ResultsForm::showResults(const ProjectList::Project &project)
{
if (currentScan.process == 0) {
ui->progressBar->setVisible(false);
resultsmodel->saveIfModified();
resultsmodel->load(lastResultFile(project.name), project.path);
}
}
void ResultsForm::triage(QModelIndex index)
{
const ResultsModel::Node node = resultsmodel->getNodeFromIndex(index);
if (node.filename.isEmpty()) {
ui->codeBrowser->clear();
ui->falsePositive->setEnabled(false);
ui->truePositive->setEnabled(false);
triageIndex = QModelIndex();
} else {
QFile file(node.fullfilename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
ui->codeBrowser->clear();
ui->falsePositive->setEnabled(false);
ui->truePositive->setEnabled(false);
triageIndex = QModelIndex();
} else {
QTextStream textstream(&file);
const QString filedata(textstream.readAll());
ui->triageWidget->setVisible(true);
ui->codeBrowser->setPlainText(filedata);
ui->codeBrowser->setLine(node.line.toInt());
ui->falsePositive->setEnabled(true);
ui->truePositive->setEnabled(true);
triageIndex = index;
}
}
}
void ResultsForm::falsePositive()
{
resultsmodel->triage(triageIndex,ResultsModel::Node::FALSE_POSITIVE);
}
void ResultsForm::truePositive()
{
resultsmodel->triage(triageIndex,ResultsModel::Node::TRUE_POSITIVE);
}