Skip to content

Commit 6787144

Browse files
committed
GUI: Refactor report saving.
Refactoring report writing to own classes and using QT's XML classes for XML output. This also fixes the ticket #cppcheck-opensource#408 (GUI generates invalid xml). https://sourceforge.net/apps/trac/cppcheck/ticket/408
1 parent df24144 commit 6787144

10 files changed

Lines changed: 481 additions & 64 deletions

File tree

gui/gui.pro

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ HEADERS += mainwindow.h \
2929
common.h \
3030
fileviewdialog.h \
3131
projectfile.h \
32+
report.h \
33+
txtreport.h \
34+
xmlreport.h \
3235
../src/checkautovariables.h \
3336
../src/checkdangerousfunctions.h \
3437
../src/checkheaders.h \
@@ -65,6 +68,9 @@ SOURCES += main.cpp \
6568
aboutdialog.cpp \
6669
fileviewdialog.cpp \
6770
projectfile.cpp \
71+
report.cpp \
72+
txtreport.cpp \
73+
xmlreport.cpp \
6874
../src/checkautovariables.cpp \
6975
../src/checkdangerousfunctions.cpp \
7076
../src/checkmemoryleak.cpp \

gui/report.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2009 Daniel Marjamäki and Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/
17+
*/
18+
19+
#include <QFile>
20+
#include "report.h"
21+
22+
Report::Report(const QString &filename, QObject * parent) :
23+
QObject(parent),
24+
mFilename(filename)
25+
{
26+
}
27+
28+
Report::~Report()
29+
{
30+
Close();
31+
}
32+
33+
bool Report::Create()
34+
{
35+
bool succeed = false;
36+
if (!mFile.isOpen())
37+
{
38+
mFile.setFileName(mFilename);
39+
succeed = mFile.open(QIODevice::WriteOnly | QIODevice::Text);
40+
}
41+
return succeed;
42+
}
43+
44+
void Report::Close()
45+
{
46+
if (mFile.isOpen())
47+
mFile.close();
48+
}
49+
50+
QFile* Report::GetFile()
51+
{
52+
return &mFile;
53+
}

gui/report.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2009 Daniel Marjamäki and Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/
17+
*/
18+
19+
#ifndef _REPORT_H_
20+
#define _REPORT_H_
21+
22+
#include <QObject>
23+
#include <QString>
24+
#include <QStringList>
25+
#include <QFile>
26+
27+
/**
28+
* @brief A base class for reports.
29+
*/
30+
class Report : public QObject
31+
{
32+
public:
33+
Report(const QString &filename, QObject * parent = 0);
34+
virtual ~Report();
35+
36+
/**
37+
* @brief Create the report (file).
38+
* @return true if succeeded, false if file could not be created.
39+
*/
40+
virtual bool Create();
41+
42+
/**
43+
* @brief Close the report (file).
44+
*/
45+
virtual void Close();
46+
47+
/**
48+
* @brief Write report header.
49+
*/
50+
virtual void WriteHeader() = 0;
51+
52+
/**
53+
* @brief Write report footer.
54+
*/
55+
virtual void WriteFooter() = 0;
56+
57+
/**
58+
* @brief Write error to report.
59+
*/
60+
virtual void WriteError(const QStringList &files, const QStringList &lines,
61+
const QString &id, const QString &severity,
62+
const QString &msg) = 0;
63+
64+
protected:
65+
66+
/**
67+
* @brief Get the file object where the report is written to.
68+
*/
69+
QFile* GetFile();
70+
71+
private:
72+
73+
/**
74+
* @brief Filename of the report.
75+
*/
76+
QString mFilename;
77+
78+
/**
79+
* @brief Fileobject for the report file.
80+
*/
81+
QFile mFile;
82+
};
83+
84+
#endif // _REPORT_H_

gui/resultstree.cpp

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/
1717
*/
1818

19-
20-
#include "resultstree.h"
2119
#include <QApplication>
2220
#include <QDebug>
2321
#include <QMenu>
@@ -27,6 +25,8 @@
2725
#include <QMessageBox>
2826
#include <QFileInfo>
2927
#include <QClipboard>
28+
#include "resultstree.h"
29+
#include "xmlreport.h"
3030

3131
ResultsTree::ResultsTree(QSettings &settings, ApplicationList &list) :
3232
mSettings(settings),
@@ -533,26 +533,20 @@ QString ResultsTree::SeverityToIcon(const QString &severity)
533533
return "";
534534
}
535535

536-
void ResultsTree::SaveResults(QTextStream &out, bool xml)
536+
void ResultsTree::SaveResults(Report *report)
537537
{
538-
if (xml)
539-
{
540-
out << "<?xml version=\"1.0\"?>" << endl << "<results>" << endl;
541-
}
538+
report->WriteHeader();
542539

543540
for (int i = 0;i < mModel.rowCount();i++)
544541
{
545542
QStandardItem *item = mModel.item(i, 0);
546-
SaveErrors(out, item, xml);
543+
SaveErrors(report, item);
547544
}
548545

549-
if (xml)
550-
{
551-
out << "</results>" << endl;
552-
}
546+
report->WriteFooter();
553547
}
554548

555-
void ResultsTree::SaveErrors(QTextStream &out, QStandardItem *item, bool xml)
549+
void ResultsTree::SaveErrors(Report *report, QStandardItem *item)
556550
{
557551
if (!item)
558552
{
@@ -592,47 +586,14 @@ void ResultsTree::SaveErrors(QTextStream &out, QStandardItem *item, bool xml)
592586
continue;
593587
}
594588

589+
for (int i = 0; i < files.count(); i++)
590+
files[i] = StripPath(files[i], true);
595591

592+
QStringList linesStr;
593+
for (int i = 0; i < lines.count(); i++)
594+
linesStr << lines[i].toString();
596595

597-
if (xml)
598-
{
599-
/*
600-
Error example from the core program in xml
601-
<error file="gui/test.cpp" line="14" id="mismatchAllocDealloc" severity="error" msg="Mismatching allocation and deallocation: k"/>
602-
The callstack seems to be ignored here aswell, instead last item of the stack is used
603-
*/
604-
line = QString("<error file=\"%1\" line=\"%2\" id=\"%3\" severity=\"%4\" msg=\"%5\"/>").
605-
arg(StripPath(files[files.size()-1], true)). //filename
606-
arg(lines[lines.size()-1].toInt()). //line
607-
arg(id). //ID
608-
arg(severity). //severity
609-
arg(message); //Message
610-
611-
}
612-
else
613-
{
614-
/*
615-
Error example from the core program in text
616-
[gui/test.cpp:23] -> [gui/test.cpp:14]: (error) Mismatching allocation and deallocation: k
617-
*/
618-
for (int i = 0;i < lines.size();i++)
619-
{
620-
line += QString("[%1:%2]").arg(StripPath(files[i], true)).arg(lines[i].toInt());
621-
if (i < lines.size() - 1 && lines.size() > 0)
622-
{
623-
line += " -> ";
624-
}
625-
626-
if (i == lines.size() - 1)
627-
{
628-
line += ": ";
629-
}
630-
}
631-
632-
line += QString("(%1) %2").arg(severity).arg(message);
633-
}
634-
635-
out << line << endl;
596+
report->WriteError(files, linesStr, id, severity, message);
636597
}
637598
}
638599

gui/resultstree.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "applicationlist.h"
3030
#include <QTextStream>
3131

32+
class Report;
33+
3234
/**
3335
* @brief Cppcheck's results are shown in this tree
3436
*
@@ -75,7 +77,7 @@ class ResultsTree : public QTreeView
7577
* @brief Save results to a text stream
7678
*
7779
*/
78-
void SaveResults(QTextStream &out, bool xml);
80+
void SaveResults(Report *report);
7981

8082
/**
8183
* @brief Update tree settings
@@ -162,9 +164,8 @@ protected slots:
162164
* @brief Save all errors under spesified item
163165
*
164166
* @param item Item whose errors to save
165-
* @param xml Should errors be saved as xml (true) or as text (false)
166167
*/
167-
void SaveErrors(QTextStream &out, QStandardItem *item, bool xml);
168+
void SaveErrors(Report *report, QStandardItem *item);
168169

169170
/**
170171
* @brief Convert a severity string to a icon filename

gui/resultsview.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/
1717
*/
1818

19-
20-
#include "resultsview.h"
2119
#include <QDebug>
2220
#include <QVBoxLayout>
2321
#include <QFile>
2422
#include <QMessageBox>
23+
#include "resultsview.h"
24+
#include "txtreport.h"
25+
#include "xmlreport.h"
2526

2627
ResultsView::ResultsView(QSettings &settings, ApplicationList &list) :
2728
mErrorsFound(false),
@@ -136,17 +137,32 @@ void ResultsView::Save(const QString &filename, bool xml)
136137
msgBox.exec();
137138
}
138139

139-
QFile file(filename);
140-
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
140+
if (xml)
141141
{
142-
return;
142+
XmlReport report(filename);
143+
if (report.Create())
144+
mTree->SaveResults(&report);
145+
else
146+
{
147+
QMessageBox msgBox;
148+
msgBox.setText("Failed to save the report.");
149+
msgBox.exec();
150+
}
151+
}
152+
else
153+
{
154+
TxtReport report(filename);
155+
if (report.Create())
156+
mTree->SaveResults(&report);
157+
else
158+
{
159+
QMessageBox msgBox;
160+
msgBox.setText("Failed to save the report.");
161+
msgBox.exec();
162+
}
143163
}
144-
145-
QTextStream out(&file);
146-
mTree->SaveResults(out, xml);
147164
}
148165

149-
150166
void ResultsView::UpdateSettings(bool showFullPath,
151167
bool saveFullPath,
152168
bool saveAllErrors,

0 commit comments

Comments
 (0)