2009-06-24 09:54:56 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2010-04-13 21:23:17 +02:00
|
|
|
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
|
2009-06-24 09:54:56 +02: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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-06-24 09:54:56 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QXmlStreamWriter>
|
2010-07-10 15:37:36 +02:00
|
|
|
#include <qdebug>
|
2010-07-11 01:02:08 +02:00
|
|
|
#include "erroritem.h"
|
2009-06-24 09:54:56 +02:00
|
|
|
#include "xmlreport.h"
|
|
|
|
|
2010-07-10 15:37:36 +02:00
|
|
|
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";
|
|
|
|
|
2009-06-24 09:54:56 +02:00
|
|
|
XmlReport::XmlReport(const QString &filename, QObject * parent) :
|
2010-07-10 12:53:44 +02:00
|
|
|
Report(filename, parent),
|
2010-07-10 15:37:36 +02:00
|
|
|
mXmlReader(NULL),
|
2010-07-10 12:53:44 +02:00
|
|
|
mXmlWriter(NULL)
|
2009-06-24 09:54:56 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
XmlReport::~XmlReport()
|
|
|
|
{
|
2010-07-10 15:37:36 +02:00
|
|
|
delete mXmlReader;
|
2010-07-10 12:53:44 +02:00
|
|
|
delete mXmlWriter;
|
2009-06-24 09:54:56 +02:00
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool XmlReport::Create()
|
|
|
|
{
|
|
|
|
bool success = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Report::Create())
|
2009-06-24 09:54:56 +02:00
|
|
|
{
|
2010-07-10 12:53:44 +02:00
|
|
|
mXmlWriter = new QXmlStreamWriter(Report::GetFile());
|
2009-06-24 09:54:56 +02:00
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2010-07-10 15:37:36 +02:00
|
|
|
bool XmlReport::Open()
|
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
if (Report::Open())
|
|
|
|
{
|
|
|
|
mXmlReader = new QXmlStreamReader(Report::GetFile());
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2009-06-24 09:54:56 +02:00
|
|
|
void XmlReport::WriteHeader()
|
|
|
|
{
|
2010-07-10 12:53:44 +02:00
|
|
|
mXmlWriter->setAutoFormatting(true);
|
|
|
|
mXmlWriter->writeStartDocument();
|
2010-07-10 15:37:36 +02:00
|
|
|
mXmlWriter->writeStartElement(ResultElementName);
|
2009-06-24 09:54:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReport::WriteFooter()
|
|
|
|
{
|
2010-07-10 12:53:44 +02:00
|
|
|
mXmlWriter->writeEndElement();
|
|
|
|
mXmlWriter->writeEndDocument();
|
2009-06-24 09:54:56 +02:00
|
|
|
}
|
|
|
|
|
2010-07-10 17:20:45 +02:00
|
|
|
void XmlReport::WriteError(const ErrorItem &error)
|
2009-06-24 09:54:56 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
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 aswell, instead last item of the stack is used
|
|
|
|
*/
|
|
|
|
|
2010-07-10 15:37:36 +02:00
|
|
|
mXmlWriter->writeStartElement(ErrorElementName);
|
2010-07-10 17:20:45 +02:00
|
|
|
mXmlWriter->writeAttribute(FilenameAttribute, error.files[error.files.size() - 1]);
|
2010-07-10 19:54:33 +02:00
|
|
|
const QString line = QString::number(error.lines[error.lines.size() - 1]);
|
|
|
|
mXmlWriter->writeAttribute(LineAttribute, line);
|
2010-07-10 17:20:45 +02:00
|
|
|
mXmlWriter->writeAttribute(IdAttribute, error.id);
|
|
|
|
mXmlWriter->writeAttribute(SeverityAttribute, error.severity);
|
|
|
|
mXmlWriter->writeAttribute(MsgAttribute, error.msg);
|
2010-07-10 12:53:44 +02:00
|
|
|
mXmlWriter->writeEndElement();
|
2009-06-24 09:54:56 +02:00
|
|
|
}
|
2010-07-10 15:37:36 +02:00
|
|
|
|
2010-07-11 01:02:08 +02:00
|
|
|
QList<ErrorLine> XmlReport::Read()
|
2010-07-10 15:37:36 +02:00
|
|
|
{
|
2010-07-11 01:02:08 +02:00
|
|
|
QList<ErrorLine> errors;
|
2010-07-10 15:37:36 +02:00
|
|
|
bool insideResults = false;
|
|
|
|
if (!mXmlReader)
|
|
|
|
{
|
|
|
|
qDebug() << "You must Open() the file before reading it!";
|
2010-07-11 01:02:08 +02:00
|
|
|
return errors;
|
2010-07-10 15:37:36 +02:00
|
|
|
}
|
|
|
|
while (!mXmlReader->atEnd())
|
|
|
|
{
|
|
|
|
switch (mXmlReader->readNext())
|
|
|
|
{
|
|
|
|
case QXmlStreamReader::StartElement:
|
|
|
|
if (mXmlReader->name() == ResultElementName)
|
|
|
|
insideResults = true;
|
|
|
|
|
|
|
|
// Read error element from inside result element
|
|
|
|
if (insideResults && mXmlReader->name() == ErrorElementName)
|
2010-07-11 01:02:08 +02:00
|
|
|
{
|
|
|
|
ErrorLine line = ReadError(mXmlReader);
|
|
|
|
errors.append(line);
|
|
|
|
}
|
2010-07-10 15:37:36 +02: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;
|
|
|
|
}
|
|
|
|
}
|
2010-07-11 01:02:08 +02:00
|
|
|
return errors;
|
2010-07-10 15:37:36 +02:00
|
|
|
}
|
|
|
|
|
2010-07-11 01:02:08 +02:00
|
|
|
ErrorLine XmlReport::ReadError(QXmlStreamReader *reader)
|
2010-07-10 15:37:36 +02:00
|
|
|
{
|
2010-07-11 01:02:08 +02:00
|
|
|
ErrorLine line;
|
2010-07-10 15:37:36 +02:00
|
|
|
if (reader->name().toString() == ErrorElementName)
|
|
|
|
{
|
|
|
|
QXmlStreamAttributes attribs = reader->attributes();
|
2010-07-11 01:02:08 +02:00
|
|
|
line.file = attribs.value("", FilenameAttribute).toString();
|
|
|
|
line.line = attribs.value("", LineAttribute).toString();
|
|
|
|
line.id = attribs.value("", IdAttribute).toString();
|
|
|
|
line.severity = attribs.value("", SeverityAttribute).toString();
|
|
|
|
line.msg = attribs.value("", MsgAttribute).toString();
|
2010-07-10 15:37:36 +02:00
|
|
|
}
|
2010-07-11 01:02:08 +02:00
|
|
|
return line;
|
2010-07-10 15:37:36 +02:00
|
|
|
}
|