2011-02-03 20:40:35 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2020-10-03 09:15:56 +02:00
|
|
|
* Copyright (C) 2007-2020 Cppcheck team.
|
2011-02-03 20:40:35 +01:00
|
|
|
*
|
|
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QString>
|
|
|
|
#include <QList>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QXmlStreamWriter>
|
|
|
|
#include <QDebug>
|
|
|
|
#include "report.h"
|
|
|
|
#include "erroritem.h"
|
2011-02-04 23:23:25 +01:00
|
|
|
#include "xmlreport.h"
|
2011-02-03 20:40:35 +01:00
|
|
|
#include "xmlreportv2.h"
|
|
|
|
#include "cppcheck.h"
|
|
|
|
|
2017-08-03 12:39:31 +02:00
|
|
|
static const QString ResultElementName = "results";
|
|
|
|
static const QString CppcheckElementName = "cppcheck";
|
|
|
|
static const QString ErrorElementName = "error";
|
|
|
|
static const QString ErrorsElementName = "errors";
|
|
|
|
static const QString LocationElementName = "location";
|
|
|
|
static const QString CWEAttribute = "cwe";
|
2020-07-21 11:27:03 +02:00
|
|
|
static const QString HashAttribute = "hash";
|
2017-08-03 12:39:31 +02:00
|
|
|
static const QString SinceDateAttribute = "sinceDate";
|
2017-08-18 17:25:08 +02:00
|
|
|
static const QString TagsAttribute = "tag";
|
2017-08-03 12:39:31 +02:00
|
|
|
static const QString FilenameAttribute = "file";
|
|
|
|
static const QString IncludedFromFilenameAttribute = "file0";
|
|
|
|
static const QString InconclusiveAttribute = "inconclusive";
|
|
|
|
static const QString InfoAttribute = "info";
|
|
|
|
static const QString LineAttribute = "line";
|
2019-08-18 12:19:05 +02:00
|
|
|
static const QString ColumnAttribute = "column";
|
2017-08-03 12:39:31 +02:00
|
|
|
static const QString IdAttribute = "id";
|
|
|
|
static const QString SeverityAttribute = "severity";
|
|
|
|
static const QString MsgAttribute = "msg";
|
|
|
|
static const QString VersionAttribute = "version";
|
|
|
|
static const QString VerboseAttribute = "verbose";
|
2011-02-03 20:40:35 +01:00
|
|
|
|
2012-10-27 12:22:56 +02:00
|
|
|
XmlReportV2::XmlReportV2(const QString &filename) :
|
|
|
|
XmlReport(filename),
|
2019-06-30 21:39:22 +02:00
|
|
|
mXmlReader(nullptr),
|
|
|
|
mXmlWriter(nullptr)
|
2011-02-03 20:40:35 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
XmlReportV2::~XmlReportV2()
|
|
|
|
{
|
|
|
|
delete mXmlReader;
|
|
|
|
delete mXmlWriter;
|
|
|
|
}
|
|
|
|
|
2017-07-28 11:12:05 +02:00
|
|
|
bool XmlReportV2::create()
|
2011-02-03 20:40:35 +01:00
|
|
|
{
|
2017-07-28 11:12:05 +02:00
|
|
|
if (Report::create()) {
|
|
|
|
mXmlWriter = new QXmlStreamWriter(Report::getFile());
|
2012-10-21 10:33:11 +02:00
|
|
|
return true;
|
2011-02-03 20:40:35 +01:00
|
|
|
}
|
2012-10-21 10:33:11 +02:00
|
|
|
return false;
|
2011-02-03 20:40:35 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 11:12:05 +02:00
|
|
|
bool XmlReportV2::open()
|
2011-02-03 20:40:35 +01:00
|
|
|
{
|
2017-07-28 11:12:05 +02:00
|
|
|
if (Report::open()) {
|
|
|
|
mXmlReader = new QXmlStreamReader(Report::getFile());
|
2012-10-21 10:33:11 +02:00
|
|
|
return true;
|
2011-02-03 20:40:35 +01:00
|
|
|
}
|
2012-10-21 10:33:11 +02:00
|
|
|
return false;
|
2011-02-03 20:40:35 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 11:12:05 +02:00
|
|
|
void XmlReportV2::writeHeader()
|
2011-02-03 20:40:35 +01:00
|
|
|
{
|
|
|
|
mXmlWriter->setAutoFormatting(true);
|
|
|
|
mXmlWriter->writeStartDocument();
|
|
|
|
mXmlWriter->writeStartElement(ResultElementName);
|
|
|
|
mXmlWriter->writeAttribute(VersionAttribute, QString::number(2));
|
|
|
|
mXmlWriter->writeStartElement(CppcheckElementName);
|
|
|
|
mXmlWriter->writeAttribute(VersionAttribute, QString(CppCheck::version()));
|
|
|
|
mXmlWriter->writeEndElement();
|
|
|
|
mXmlWriter->writeStartElement(ErrorsElementName);
|
|
|
|
}
|
|
|
|
|
2017-07-28 11:12:05 +02:00
|
|
|
void XmlReportV2::writeFooter()
|
2011-02-03 20:40:35 +01:00
|
|
|
{
|
|
|
|
mXmlWriter->writeEndElement(); // errors
|
|
|
|
mXmlWriter->writeEndElement(); // results
|
|
|
|
mXmlWriter->writeEndDocument();
|
|
|
|
}
|
|
|
|
|
2017-07-28 11:12:05 +02:00
|
|
|
void XmlReportV2::writeError(const ErrorItem &error)
|
2011-02-03 20:40:35 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
Error example from the core program in xml
|
|
|
|
<error id="mismatchAllocDealloc" severity="error" msg="Mismatching allocation and deallocation: k"
|
|
|
|
verbose="Mismatching allocation and deallocation: k">
|
|
|
|
<location file="..\..\test\test.cxx" line="16"/>
|
|
|
|
<location file="..\..\test\test.cxx" line="32"/>
|
|
|
|
</error>
|
|
|
|
*/
|
|
|
|
|
|
|
|
mXmlWriter->writeStartElement(ErrorElementName);
|
2011-10-12 21:18:54 +02:00
|
|
|
mXmlWriter->writeAttribute(IdAttribute, error.errorId);
|
2011-03-07 20:10:30 +01:00
|
|
|
|
|
|
|
// Don't localize severity so we can read these files
|
|
|
|
mXmlWriter->writeAttribute(SeverityAttribute, GuiSeverity::toString(error.severity));
|
2011-02-04 23:23:25 +01:00
|
|
|
const QString summary = XmlReport::quoteMessage(error.summary);
|
|
|
|
mXmlWriter->writeAttribute(MsgAttribute, summary);
|
|
|
|
const QString message = XmlReport::quoteMessage(error.message);
|
|
|
|
mXmlWriter->writeAttribute(VerboseAttribute, message);
|
2017-05-21 08:25:55 +02:00
|
|
|
if (error.inconclusive)
|
|
|
|
mXmlWriter->writeAttribute(InconclusiveAttribute, "true");
|
|
|
|
if (error.cwe > 0)
|
|
|
|
mXmlWriter->writeAttribute(CWEAttribute, QString::number(error.cwe));
|
2020-07-21 11:27:03 +02:00
|
|
|
if (error.hash > 0)
|
|
|
|
mXmlWriter->writeAttribute(HashAttribute, QString::number(error.hash));
|
2017-08-02 20:24:23 +02:00
|
|
|
if (!error.sinceDate.isEmpty())
|
|
|
|
mXmlWriter->writeAttribute(SinceDateAttribute, error.sinceDate);
|
2017-08-18 17:25:08 +02:00
|
|
|
if (!error.tags.isEmpty())
|
|
|
|
mXmlWriter->writeAttribute(TagsAttribute, error.tags);
|
2011-02-03 20:40:35 +01:00
|
|
|
|
2017-05-21 08:25:55 +02:00
|
|
|
for (int i = error.errorPath.count() - 1; i >= 0; i--) {
|
2011-02-03 20:40:35 +01:00
|
|
|
mXmlWriter->writeStartElement(LocationElementName);
|
|
|
|
|
2017-05-21 08:25:55 +02:00
|
|
|
QString file = QDir::toNativeSeparators(error.errorPath[i].file);
|
2016-01-19 07:01:21 +01:00
|
|
|
if (!error.file0.isEmpty() && file != error.file0) {
|
2016-01-15 16:52:22 +01:00
|
|
|
mXmlWriter->writeAttribute(IncludedFromFilenameAttribute, quoteMessage(error.file0));
|
|
|
|
}
|
2017-05-21 08:25:55 +02:00
|
|
|
mXmlWriter->writeAttribute(FilenameAttribute, XmlReport::quoteMessage(file));
|
|
|
|
mXmlWriter->writeAttribute(LineAttribute, QString::number(error.errorPath[i].line));
|
2019-08-18 12:19:05 +02:00
|
|
|
if (error.errorPath[i].column > 0)
|
|
|
|
mXmlWriter->writeAttribute(ColumnAttribute, QString::number(error.errorPath[i].column));
|
2017-05-21 08:25:55 +02:00
|
|
|
if (error.errorPath.count() > 1)
|
|
|
|
mXmlWriter->writeAttribute(InfoAttribute, XmlReport::quoteMessage(error.errorPath[i].info));
|
2011-02-03 20:40:35 +01:00
|
|
|
|
|
|
|
mXmlWriter->writeEndElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
mXmlWriter->writeEndElement();
|
|
|
|
}
|
|
|
|
|
2017-07-28 11:12:05 +02:00
|
|
|
QList<ErrorItem> XmlReportV2::read()
|
2011-02-03 20:40:35 +01:00
|
|
|
{
|
2011-02-05 11:41:29 +01:00
|
|
|
QList<ErrorItem> errors;
|
2011-02-03 20:40:35 +01:00
|
|
|
bool insideResults = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!mXmlReader) {
|
2011-02-03 20:40:35 +01:00
|
|
|
qDebug() << "You must Open() the file before reading it!";
|
|
|
|
return errors;
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
while (!mXmlReader->atEnd()) {
|
|
|
|
switch (mXmlReader->readNext()) {
|
2011-02-03 20:40:35 +01:00
|
|
|
case QXmlStreamReader::StartElement:
|
|
|
|
if (mXmlReader->name() == ResultElementName)
|
|
|
|
insideResults = true;
|
|
|
|
|
|
|
|
// Read error element from inside result element
|
2011-10-13 20:53:06 +02:00
|
|
|
if (insideResults && mXmlReader->name() == ErrorElementName) {
|
2017-07-28 11:12:05 +02:00
|
|
|
ErrorItem item = readError(mXmlReader);
|
2011-02-05 11:41:29 +01:00
|
|
|
errors.append(item);
|
2011-02-03 20:40:35 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QXmlStreamReader::EndElement:
|
|
|
|
if (mXmlReader->name() == ResultElementName)
|
|
|
|
insideResults = false;
|
|
|
|
break;
|
|
|
|
|
2015-12-17 15:40:54 +01:00
|
|
|
// Not handled
|
2011-02-03 20:40:35 +01:00
|
|
|
case QXmlStreamReader::NoToken:
|
|
|
|
case QXmlStreamReader::Invalid:
|
|
|
|
case QXmlStreamReader::StartDocument:
|
|
|
|
case QXmlStreamReader::EndDocument:
|
|
|
|
case QXmlStreamReader::Characters:
|
|
|
|
case QXmlStreamReader::Comment:
|
|
|
|
case QXmlStreamReader::DTD:
|
|
|
|
case QXmlStreamReader::EntityReference:
|
|
|
|
case QXmlStreamReader::ProcessingInstruction:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
2017-07-28 11:12:05 +02:00
|
|
|
ErrorItem XmlReportV2::readError(QXmlStreamReader *reader)
|
2011-02-03 20:40:35 +01:00
|
|
|
{
|
2011-02-05 14:56:56 +01:00
|
|
|
/*
|
|
|
|
Error example from the core program in xml
|
|
|
|
<error id="mismatchAllocDealloc" severity="error" msg="Mismatching allocation and deallocation: k"
|
|
|
|
verbose="Mismatching allocation and deallocation: k">
|
|
|
|
<location file="..\..\test\test.cxx" line="16"/>
|
|
|
|
<location file="..\..\test\test.cxx" line="32"/>
|
|
|
|
</error>
|
|
|
|
*/
|
|
|
|
|
2011-02-05 11:41:29 +01:00
|
|
|
ErrorItem item;
|
2011-02-05 17:33:45 +01:00
|
|
|
|
|
|
|
// Read error element from inside errors element
|
2011-10-13 20:53:06 +02:00
|
|
|
if (mXmlReader->name() == ErrorElementName) {
|
2011-02-03 20:40:35 +01:00
|
|
|
QXmlStreamAttributes attribs = reader->attributes();
|
2017-08-03 12:39:31 +02:00
|
|
|
item.errorId = attribs.value(QString(), IdAttribute).toString();
|
|
|
|
item.severity = GuiSeverity::fromString(attribs.value(QString(), SeverityAttribute).toString());
|
|
|
|
const QString summary = attribs.value(QString(), MsgAttribute).toString();
|
2011-02-05 15:07:59 +01:00
|
|
|
item.summary = XmlReport::unquoteMessage(summary);
|
2017-08-03 12:39:31 +02:00
|
|
|
const QString message = attribs.value(QString(), VerboseAttribute).toString();
|
2011-02-05 15:07:59 +01:00
|
|
|
item.message = XmlReport::unquoteMessage(message);
|
2017-08-03 12:39:31 +02:00
|
|
|
if (attribs.hasAttribute(QString(), InconclusiveAttribute))
|
2017-05-21 08:25:55 +02:00
|
|
|
item.inconclusive = true;
|
2017-08-03 12:39:31 +02:00
|
|
|
if (attribs.hasAttribute(QString(), CWEAttribute))
|
2020-07-15 07:59:05 +02:00
|
|
|
item.cwe = attribs.value(QString(), CWEAttribute).toInt();
|
2020-07-21 11:27:03 +02:00
|
|
|
if (attribs.hasAttribute(QString(), HashAttribute))
|
|
|
|
item.hash = attribs.value(QString(), HashAttribute).toULongLong();
|
2017-08-03 12:39:31 +02:00
|
|
|
if (attribs.hasAttribute(QString(), SinceDateAttribute))
|
|
|
|
item.sinceDate = attribs.value(QString(), SinceDateAttribute).toString();
|
2017-08-18 17:25:08 +02:00
|
|
|
if (attribs.hasAttribute(QString(), TagsAttribute))
|
|
|
|
item.tags = attribs.value(QString(), TagsAttribute).toString();
|
2011-02-03 20:40:35 +01:00
|
|
|
}
|
2011-02-05 14:56:56 +01:00
|
|
|
|
2011-02-05 17:33:45 +01:00
|
|
|
bool errorRead = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (!errorRead && !mXmlReader->atEnd()) {
|
|
|
|
switch (mXmlReader->readNext()) {
|
2011-02-05 14:56:56 +01:00
|
|
|
case QXmlStreamReader::StartElement:
|
2011-02-05 17:33:45 +01:00
|
|
|
|
|
|
|
// Read location element from inside error element
|
2011-10-13 20:53:06 +02:00
|
|
|
if (mXmlReader->name() == LocationElementName) {
|
2011-02-05 14:56:56 +01:00
|
|
|
QXmlStreamAttributes attribs = mXmlReader->attributes();
|
2017-08-03 12:39:31 +02:00
|
|
|
QString file0 = attribs.value(QString(), IncludedFromFilenameAttribute).toString();
|
2016-01-15 16:52:22 +01:00
|
|
|
if (!file0.isEmpty())
|
|
|
|
item.file0 = XmlReport::unquoteMessage(file0);
|
2017-05-21 08:25:55 +02:00
|
|
|
QErrorPathItem loc;
|
2017-08-03 12:39:31 +02:00
|
|
|
loc.file = XmlReport::unquoteMessage(attribs.value(QString(), FilenameAttribute).toString());
|
|
|
|
loc.line = attribs.value(QString(), LineAttribute).toString().toUInt();
|
2019-08-18 12:19:05 +02:00
|
|
|
if (attribs.hasAttribute(QString(), ColumnAttribute))
|
|
|
|
loc.column = attribs.value(QString(), ColumnAttribute).toString().toInt();
|
2017-08-03 12:39:31 +02:00
|
|
|
if (attribs.hasAttribute(QString(), InfoAttribute))
|
|
|
|
loc.info = XmlReport::unquoteMessage(attribs.value(QString(), InfoAttribute).toString());
|
2017-05-21 08:25:55 +02:00
|
|
|
item.errorPath.push_front(loc);
|
2011-02-05 14:56:56 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QXmlStreamReader::EndElement:
|
2011-02-05 17:33:45 +01:00
|
|
|
if (mXmlReader->name() == ErrorElementName)
|
|
|
|
errorRead = true;
|
2011-02-05 14:56:56 +01:00
|
|
|
break;
|
|
|
|
|
2015-12-17 15:40:54 +01:00
|
|
|
// Not handled
|
2011-02-05 14:56:56 +01:00
|
|
|
case QXmlStreamReader::NoToken:
|
|
|
|
case QXmlStreamReader::Invalid:
|
|
|
|
case QXmlStreamReader::StartDocument:
|
|
|
|
case QXmlStreamReader::EndDocument:
|
|
|
|
case QXmlStreamReader::Characters:
|
|
|
|
case QXmlStreamReader::Comment:
|
|
|
|
case QXmlStreamReader::DTD:
|
|
|
|
case QXmlStreamReader::EntityReference:
|
|
|
|
case QXmlStreamReader::ProcessingInstruction:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-08-02 20:24:23 +02:00
|
|
|
|
|
|
|
if (item.errorPath.size() == 1 && item.errorPath[0].info.isEmpty())
|
|
|
|
item.errorPath[0].info = item.message;
|
|
|
|
|
2011-02-05 17:33:45 +01:00
|
|
|
return item;
|
2011-02-05 14:56:56 +01:00
|
|
|
}
|