2011-02-03 20:40:35 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2012-01-01 00:05:37 +01:00
|
|
|
* Copyright (C) 2007-2012 Daniel Marjamäki and 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"
|
|
|
|
|
|
|
|
static const char ResultElementName[] = "results";
|
|
|
|
static const char CppcheckElementName[] = "cppcheck";
|
|
|
|
static const char ErrorElementName[] = "error";
|
|
|
|
static const char ErrorsElementName[] = "errors";
|
|
|
|
static const char LocationElementName[] = "location";
|
|
|
|
static const char FilenameAttribute[] = "file";
|
|
|
|
static const char LineAttribute[] = "line";
|
|
|
|
static const char IdAttribute[] = "id";
|
|
|
|
static const char SeverityAttribute[] = "severity";
|
|
|
|
static const char MsgAttribute[] = "msg";
|
|
|
|
static const char VersionAttribute[] = "version";
|
|
|
|
static const char VerboseAttribute[] = "verbose";
|
|
|
|
|
2012-10-27 12:22:56 +02:00
|
|
|
XmlReportV2::XmlReportV2(const QString &filename) :
|
|
|
|
XmlReport(filename),
|
2011-02-03 20:40:35 +01:00
|
|
|
mXmlReader(NULL),
|
|
|
|
mXmlWriter(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
XmlReportV2::~XmlReportV2()
|
|
|
|
{
|
|
|
|
delete mXmlReader;
|
|
|
|
delete mXmlWriter;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool XmlReportV2::Create()
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Report::Create()) {
|
2011-02-03 20:40:35 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool XmlReportV2::Open()
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Report::Open()) {
|
2011-02-03 20:40:35 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReportV2::WriteHeader()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReportV2::WriteFooter()
|
|
|
|
{
|
|
|
|
mXmlWriter->writeEndElement(); // errors
|
|
|
|
mXmlWriter->writeEndElement(); // results
|
|
|
|
mXmlWriter->writeEndDocument();
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReportV2::WriteError(const ErrorItem &error)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
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-04-16 13:04:20 +02:00
|
|
|
// Don't write inconclusive errors to XML V2 until we decide the format
|
|
|
|
if (error.inconclusive)
|
|
|
|
return;
|
|
|
|
|
2011-02-03 20:40:35 +01:00
|
|
|
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);
|
2011-02-03 20:40:35 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (int i = 0; i < error.files.count(); i++) {
|
2011-02-03 20:40:35 +01:00
|
|
|
mXmlWriter->writeStartElement(LocationElementName);
|
|
|
|
|
2011-02-04 23:23:25 +01:00
|
|
|
QString file = QDir::toNativeSeparators(error.files[i]);
|
|
|
|
file = XmlReport::quoteMessage(file);
|
2011-02-03 20:40:35 +01:00
|
|
|
mXmlWriter->writeAttribute(FilenameAttribute, file);
|
|
|
|
const QString line = QString::number(error.lines[i]);
|
|
|
|
mXmlWriter->writeAttribute(LineAttribute, line);
|
|
|
|
|
|
|
|
mXmlWriter->writeEndElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
mXmlWriter->writeEndElement();
|
|
|
|
}
|
|
|
|
|
2011-02-05 11:41:29 +01: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) {
|
2011-02-05 11:41:29 +01:00
|
|
|
ErrorItem item = ReadError(mXmlReader);
|
|
|
|
errors.append(item);
|
2011-02-03 20:40:35 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QXmlStreamReader::EndElement:
|
|
|
|
if (mXmlReader->name() == ResultElementName)
|
|
|
|
insideResults = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Not handled
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-02-05 11:41:29 +01: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();
|
2011-10-12 21:18:54 +02:00
|
|
|
item.errorId = attribs.value("", IdAttribute).toString();
|
2011-03-07 20:10:30 +01:00
|
|
|
item.severity = GuiSeverity::fromString(attribs.value("", SeverityAttribute).toString());
|
2011-02-05 15:07:59 +01:00
|
|
|
const QString summary = attribs.value("", MsgAttribute).toString();
|
|
|
|
item.summary = XmlReport::unquoteMessage(summary);
|
|
|
|
const QString message = attribs.value("", VerboseAttribute).toString();
|
|
|
|
item.message = XmlReport::unquoteMessage(message);
|
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();
|
2011-02-05 15:07:59 +01:00
|
|
|
QString file = attribs.value("", FilenameAttribute).toString();
|
|
|
|
file = XmlReport::unquoteMessage(file);
|
2011-02-05 14:56:56 +01:00
|
|
|
if (item.file.isEmpty())
|
|
|
|
item.file = file;
|
|
|
|
item.files.push_back(file);
|
|
|
|
const int line = attribs.value("", LineAttribute).toString().toUInt();
|
|
|
|
item.lines.push_back(line);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Not handled
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2011-02-05 17:33:45 +01:00
|
|
|
return item;
|
2011-02-05 14:56:56 +01:00
|
|
|
}
|