2011-02-04 22:56:14 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2013-01-01 17:29:08 +01:00
|
|
|
* Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
|
2011-02-04 22:56:14 +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-04 22:56:14 +01:00
|
|
|
#include "xmlreportv1.h"
|
|
|
|
|
|
|
|
static const char ResultElementName[] = "results";
|
|
|
|
static const char ErrorElementName[] = "error";
|
|
|
|
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";
|
|
|
|
|
2012-10-27 12:22:56 +02:00
|
|
|
XmlReportV1::XmlReportV1(const QString &filename) :
|
|
|
|
XmlReport(filename),
|
2011-02-04 22:56:14 +01:00
|
|
|
mXmlReader(NULL),
|
|
|
|
mXmlWriter(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
XmlReportV1::~XmlReportV1()
|
|
|
|
{
|
|
|
|
delete mXmlReader;
|
|
|
|
delete mXmlWriter;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool XmlReportV1::Create()
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Report::Create()) {
|
2011-02-04 22:56:14 +01:00
|
|
|
mXmlWriter = new QXmlStreamWriter(Report::GetFile());
|
2012-10-21 10:33:11 +02:00
|
|
|
return true;
|
2011-02-04 22:56:14 +01:00
|
|
|
}
|
2012-10-21 10:33:11 +02:00
|
|
|
return false;
|
2011-02-04 22:56:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool XmlReportV1::Open()
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Report::Open()) {
|
2011-02-04 22:56:14 +01:00
|
|
|
mXmlReader = new QXmlStreamReader(Report::GetFile());
|
2012-10-21 10:33:11 +02:00
|
|
|
return true;
|
2011-02-04 22:56:14 +01:00
|
|
|
}
|
2012-10-21 10:33:11 +02:00
|
|
|
return false;
|
2011-02-04 22:56:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReportV1::WriteHeader()
|
|
|
|
{
|
|
|
|
mXmlWriter->setAutoFormatting(true);
|
|
|
|
mXmlWriter->writeStartDocument();
|
|
|
|
mXmlWriter->writeStartElement(ResultElementName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReportV1::WriteFooter()
|
|
|
|
{
|
|
|
|
mXmlWriter->writeEndElement();
|
|
|
|
mXmlWriter->writeEndDocument();
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReportV1::WriteError(const ErrorItem &error)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Error example from the core program in xml
|
|
|
|
<error file="gui/test.cpp" line="14" id="mismatchAllocDealloc" severity="error" msg="Mismatching allocation and deallocation: k"/>
|
|
|
|
The callstack seems to be ignored here as well, instead last item of the stack is used
|
|
|
|
*/
|
|
|
|
|
2011-04-16 13:04:20 +02:00
|
|
|
// Don't write inconclusive errors to XML V1
|
|
|
|
if (error.inconclusive)
|
|
|
|
return;
|
|
|
|
|
2011-02-04 22:56:14 +01:00
|
|
|
mXmlWriter->writeStartElement(ErrorElementName);
|
2011-02-04 23:23:25 +01:00
|
|
|
QString file = QDir::toNativeSeparators(error.files[error.files.size() - 1]);
|
|
|
|
file = XmlReport::quoteMessage(file);
|
2011-02-04 22:56:14 +01:00
|
|
|
mXmlWriter->writeAttribute(FilenameAttribute, file);
|
|
|
|
const QString line = QString::number(error.lines[error.lines.size() - 1]);
|
|
|
|
mXmlWriter->writeAttribute(LineAttribute, line);
|
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 message = XmlReport::quoteMessage(error.message);
|
|
|
|
mXmlWriter->writeAttribute(MsgAttribute, message);
|
2011-02-04 22:56:14 +01:00
|
|
|
mXmlWriter->writeEndElement();
|
|
|
|
}
|
|
|
|
|
2011-02-05 11:41:29 +01:00
|
|
|
QList<ErrorItem> XmlReportV1::Read()
|
2011-02-04 22:56:14 +01:00
|
|
|
{
|
2011-02-05 11:41:29 +01:00
|
|
|
QList<ErrorItem> errors;
|
2011-02-04 22:56:14 +01:00
|
|
|
bool insideResults = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!mXmlReader) {
|
2011-02-04 22:56:14 +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-04 22:56:14 +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-04 22:56:14 +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 XmlReportV1::ReadError(QXmlStreamReader *reader)
|
2011-02-04 22:56:14 +01:00
|
|
|
{
|
2011-02-05 11:41:29 +01:00
|
|
|
ErrorItem item;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (reader->name().toString() == ErrorElementName) {
|
2011-02-04 22:56:14 +01:00
|
|
|
QXmlStreamAttributes attribs = reader->attributes();
|
2011-02-05 15:07:59 +01:00
|
|
|
QString file = attribs.value("", FilenameAttribute).toString();
|
|
|
|
file = XmlReport::unquoteMessage(file);
|
2011-02-05 11:41:29 +01:00
|
|
|
item.file = file;
|
|
|
|
item.files.push_back(file);
|
|
|
|
const int line = attribs.value("", LineAttribute).toString().toUInt();
|
|
|
|
item.lines.push_back(line);
|
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-04 22:56:14 +01:00
|
|
|
|
|
|
|
// NOTE: This dublicates the message to Summary-field. But since
|
|
|
|
// old XML format doesn't have separate summary and verbose messages
|
|
|
|
// we must add same message to both data so it shows up in GUI.
|
|
|
|
// Check if there is full stop and cut the summary to it.
|
|
|
|
QString summary = attribs.value("", MsgAttribute).toString();
|
|
|
|
const int ind = summary.indexOf('.');
|
|
|
|
if (ind != -1)
|
|
|
|
summary = summary.left(ind + 1);
|
2011-02-05 15:07:59 +01:00
|
|
|
item.summary = XmlReport::unquoteMessage(summary);
|
|
|
|
QString message = attribs.value("", MsgAttribute).toString();
|
|
|
|
item.message = XmlReport::unquoteMessage(message);
|
2011-02-04 22:56:14 +01:00
|
|
|
}
|
2011-02-05 11:41:29 +01:00
|
|
|
return item;
|
2011-02-04 22:56:14 +01:00
|
|
|
}
|