Skip to content

Commit eaf0bca

Browse files
committed
GUI: Write XML version 2.
Ticket cppcheck-opensource#2521 (GUI: Add XML format 2 support)
1 parent 85187dc commit eaf0bca

6 files changed

Lines changed: 311 additions & 2 deletions

File tree

‎gui/gui.pro‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ HEADERS += mainwindow.h \
6969
report.h \
7070
txtreport.h \
7171
xmlreport.h \
72+
xmlreportv2.h \
7273
translationhandler.h \
7374
csvreport.h \
7475
logview.h \
@@ -96,6 +97,7 @@ SOURCES += main.cpp \
9697
report.cpp \
9798
txtreport.cpp \
9899
xmlreport.cpp \
100+
xmlreportv2.cpp \
99101
translationhandler.cpp \
100102
csvreport.cpp \
101103
logview.cpp \

‎gui/mainwindow.cpp‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ void MainWindow::ShowAuthors()
579579
void MainWindow::Save()
580580
{
581581
QString selectedFilter;
582-
QString filter(tr("XML files (*.xml);;Text files (*.txt);;CSV files (*.csv)"));
582+
QString filter(tr("XML files version 2 (*.xml);;XML files version 1 (*.xml);;Text files (*.txt);;CSV files (*.csv)"));
583583
QString selectedFile = QFileDialog::getSaveFileName(this,
584584
tr("Save the report file"),
585585
QString(),
@@ -589,12 +589,18 @@ void MainWindow::Save()
589589
if (!selectedFile.isEmpty())
590590
{
591591
Report::Type type = Report::TXT;
592-
if (selectedFilter == tr("XML files (*.xml)"))
592+
if (selectedFilter == tr("XML files version 1 (*.xml)"))
593593
{
594594
type = Report::XML;
595595
if (!selectedFile.endsWith(".xml", Qt::CaseInsensitive))
596596
selectedFile += ".xml";
597597
}
598+
else if (selectedFilter == tr("XML files version 2 (*.xml)"))
599+
{
600+
type = Report::XMLV2;
601+
if (!selectedFile.endsWith(".xml", Qt::CaseInsensitive))
602+
selectedFile += ".xml";
603+
}
598604
else if (selectedFilter == tr("Text files (*.txt)"))
599605
{
600606
type = Report::TXT;

‎gui/report.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Report : public QObject
3939
{
4040
TXT,
4141
XML,
42+
XMLV2,
4243
CSV,
4344
};
4445

‎gui/resultsview.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "report.h"
3232
#include "txtreport.h"
3333
#include "xmlreport.h"
34+
#include "xmlreportv2.h"
3435
#include "csvreport.h"
3536
#include "applicationlist.h"
3637
#include "checkstatistics.h"
@@ -132,6 +133,9 @@ void ResultsView::Save(const QString &filename, Report::Type type)
132133
case Report::XML:
133134
report = new XmlReport(filename, this);
134135
break;
136+
case Report::XMLV2:
137+
report = new XmlReportV2(filename, this);
138+
break;
135139
}
136140

137141
if (report)

‎gui/xmlreportv2.cpp‎

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2011 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 <QObject>
20+
#include <QString>
21+
#include <QList>
22+
#include <QDir>
23+
#include <QFile>
24+
#include <QXmlStreamWriter>
25+
#include <QDebug>
26+
#include "report.h"
27+
#include "erroritem.h"
28+
#include "xmlreportv2.h"
29+
#include "cppcheck.h"
30+
31+
static const char ResultElementName[] = "results";
32+
static const char CppcheckElementName[] = "cppcheck";
33+
static const char ErrorElementName[] = "error";
34+
static const char ErrorsElementName[] = "errors";
35+
static const char LocationElementName[] = "location";
36+
static const char FilenameAttribute[] = "file";
37+
static const char LineAttribute[] = "line";
38+
static const char IdAttribute[] = "id";
39+
static const char SeverityAttribute[] = "severity";
40+
static const char MsgAttribute[] = "msg";
41+
static const char VersionAttribute[] = "version";
42+
static const char VerboseAttribute[] = "verbose";
43+
44+
XmlReportV2::XmlReportV2(const QString &filename, QObject * parent) :
45+
Report(filename, parent),
46+
mXmlReader(NULL),
47+
mXmlWriter(NULL)
48+
{
49+
}
50+
51+
XmlReportV2::~XmlReportV2()
52+
{
53+
delete mXmlReader;
54+
delete mXmlWriter;
55+
Close();
56+
}
57+
58+
bool XmlReportV2::Create()
59+
{
60+
bool success = false;
61+
if (Report::Create())
62+
{
63+
mXmlWriter = new QXmlStreamWriter(Report::GetFile());
64+
success = true;
65+
}
66+
return success;
67+
}
68+
69+
bool XmlReportV2::Open()
70+
{
71+
bool success = false;
72+
if (Report::Open())
73+
{
74+
mXmlReader = new QXmlStreamReader(Report::GetFile());
75+
success = true;
76+
}
77+
return success;
78+
}
79+
80+
void XmlReportV2::WriteHeader()
81+
{
82+
mXmlWriter->setAutoFormatting(true);
83+
mXmlWriter->writeStartDocument();
84+
mXmlWriter->writeStartElement(ResultElementName);
85+
mXmlWriter->writeAttribute(VersionAttribute, QString::number(2));
86+
mXmlWriter->writeStartElement(CppcheckElementName);
87+
mXmlWriter->writeAttribute(VersionAttribute, QString(CppCheck::version()));
88+
mXmlWriter->writeEndElement();
89+
mXmlWriter->writeStartElement(ErrorsElementName);
90+
}
91+
92+
void XmlReportV2::WriteFooter()
93+
{
94+
mXmlWriter->writeEndElement(); // errors
95+
mXmlWriter->writeEndElement(); // results
96+
mXmlWriter->writeEndDocument();
97+
}
98+
99+
void XmlReportV2::WriteError(const ErrorItem &error)
100+
{
101+
/*
102+
Error example from the core program in xml
103+
<error id="mismatchAllocDealloc" severity="error" msg="Mismatching allocation and deallocation: k"
104+
verbose="Mismatching allocation and deallocation: k">
105+
<location file="..\..\test\test.cxx" line="16"/>
106+
<location file="..\..\test\test.cxx" line="32"/>
107+
</error>
108+
*/
109+
110+
mXmlWriter->writeStartElement(ErrorElementName);
111+
mXmlWriter->writeAttribute(IdAttribute, error.id);
112+
mXmlWriter->writeAttribute(SeverityAttribute, error.severity);
113+
mXmlWriter->writeAttribute(MsgAttribute, error.summary);
114+
mXmlWriter->writeAttribute(VerboseAttribute, error.message);
115+
116+
for (int i = 0; i < error.files.count(); i++)
117+
{
118+
mXmlWriter->writeStartElement(LocationElementName);
119+
120+
const QString file = QDir::toNativeSeparators(error.files[i]);
121+
mXmlWriter->writeAttribute(FilenameAttribute, file);
122+
const QString line = QString::number(error.lines[i]);
123+
mXmlWriter->writeAttribute(LineAttribute, line);
124+
125+
mXmlWriter->writeEndElement();
126+
}
127+
128+
mXmlWriter->writeEndElement();
129+
}
130+
131+
QList<ErrorLine> XmlReportV2::Read()
132+
{
133+
QList<ErrorLine> errors;
134+
bool insideResults = false;
135+
if (!mXmlReader)
136+
{
137+
qDebug() << "You must Open() the file before reading it!";
138+
return errors;
139+
}
140+
while (!mXmlReader->atEnd())
141+
{
142+
switch (mXmlReader->readNext())
143+
{
144+
case QXmlStreamReader::StartElement:
145+
if (mXmlReader->name() == ResultElementName)
146+
insideResults = true;
147+
148+
// Read error element from inside result element
149+
if (insideResults && mXmlReader->name() == ErrorElementName)
150+
{
151+
ErrorLine line = ReadError(mXmlReader);
152+
errors.append(line);
153+
}
154+
break;
155+
156+
case QXmlStreamReader::EndElement:
157+
if (mXmlReader->name() == ResultElementName)
158+
insideResults = false;
159+
break;
160+
161+
// Not handled
162+
case QXmlStreamReader::NoToken:
163+
case QXmlStreamReader::Invalid:
164+
case QXmlStreamReader::StartDocument:
165+
case QXmlStreamReader::EndDocument:
166+
case QXmlStreamReader::Characters:
167+
case QXmlStreamReader::Comment:
168+
case QXmlStreamReader::DTD:
169+
case QXmlStreamReader::EntityReference:
170+
case QXmlStreamReader::ProcessingInstruction:
171+
break;
172+
}
173+
}
174+
return errors;
175+
}
176+
177+
ErrorLine XmlReportV2::ReadError(QXmlStreamReader *reader)
178+
{
179+
ErrorLine line;
180+
if (reader->name().toString() == ErrorElementName)
181+
{
182+
QXmlStreamAttributes attribs = reader->attributes();
183+
line.file = attribs.value("", FilenameAttribute).toString();
184+
line.line = attribs.value("", LineAttribute).toString().toUInt();
185+
line.id = attribs.value("", IdAttribute).toString();
186+
line.severity = attribs.value("", SeverityAttribute).toString();
187+
188+
// NOTE: This dublicates the message to Summary-field. But since
189+
// old XML format doesn't have separate summary and verbose messages
190+
// we must add same message to both data so it shows up in GUI.
191+
// Check if there is full stop and cut the summary to it.
192+
QString summary = attribs.value("", MsgAttribute).toString();
193+
const int ind = summary.indexOf('.');
194+
if (ind != -1)
195+
summary = summary.left(ind + 1);
196+
line.summary = summary;
197+
line.message = attribs.value("", MsgAttribute).toString();
198+
}
199+
return line;
200+
}

‎gui/xmlreportv2.h‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2011 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 XML_REPORTV2_H
20+
#define XML_REPORTV2_H
21+
22+
#include <QObject>
23+
#include <QString>
24+
#include <QStringList>
25+
#include <QFile>
26+
#include <QXmlStreamReader>
27+
#include <QXmlStreamWriter>
28+
#include "report.h"
29+
30+
/// @addtogroup GUI
31+
/// @{
32+
33+
34+
/**
35+
* @brief XML file report version 2.
36+
* This report outputs XML-formatted report. The XML format must match command
37+
* line version's XML output.
38+
*/
39+
class XmlReportV2 : public Report
40+
{
41+
public:
42+
XmlReportV2(const QString &filename, QObject * parent = 0);
43+
virtual ~XmlReportV2();
44+
45+
/**
46+
* @brief Create the report (file).
47+
* @return true if succeeded, false if file could not be created.
48+
*/
49+
virtual bool Create();
50+
51+
/**
52+
* @brief Open existing report file.
53+
*/
54+
bool Open();
55+
56+
/**
57+
* @brief Write report header.
58+
*/
59+
virtual void WriteHeader();
60+
61+
/**
62+
* @brief Write report footer.
63+
*/
64+
virtual void WriteFooter();
65+
66+
/**
67+
* @brief Write error to report.
68+
* @param error Error data.
69+
*/
70+
virtual void WriteError(const ErrorItem &error);
71+
72+
/**
73+
* @brief Read contents of the report file.
74+
*/
75+
QList<ErrorLine> Read();
76+
77+
protected:
78+
/**
79+
* @brief Read and parse error item from XML stream.
80+
* @param reader XML stream reader to use.
81+
*/
82+
ErrorLine ReadError(QXmlStreamReader *reader);
83+
84+
private:
85+
/**
86+
* @brief XML stream reader for reading the report in XML format.
87+
*/
88+
QXmlStreamReader *mXmlReader;
89+
90+
/**
91+
* @brief XML stream writer for writing the report in XML format.
92+
*/
93+
QXmlStreamWriter *mXmlWriter;
94+
};
95+
/// @}
96+
#endif // XML_REPORTV2_H

0 commit comments

Comments
 (0)