|
| 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 | +} |
0 commit comments