1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2024 Cppcheck team.
*
* 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 "xmlreportv2.h"
#include "cppcheck.h"
#include "erroritem.h"
#include "report.h"
#include "settings.h"
#include "xmlreport.h"
#include <utility>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QXmlStreamAttributes>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
#include <QStringRef>
#endif
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";
static const QString HashAttribute = "hash";
static const QString SinceDateAttribute = "sinceDate";
static const QString TagsAttribute = "tag";
static const QString FilenameAttribute = "file";
static const QString IncludedFromFilenameAttribute = "file0";
static const QString InconclusiveAttribute = "inconclusive";
static const QString RemarkAttribute = "remark";
static const QString InfoAttribute = "info";
static const QString LineAttribute = "line";
static const QString ColumnAttribute = "column";
static const QString IdAttribute = "id";
static const QString SeverityAttribute = "severity";
static const QString MsgAttribute = "msg";
static const QString VersionAttribute = "version";
static const QString ProductNameAttribute = "product-name";
static const QString VerboseAttribute = "verbose";
XmlReportV2::XmlReportV2(const QString &filename, QString productName) :
XmlReport(filename),
mProductName(std::move(productName)),
mXmlReader(nullptr),
mXmlWriter(nullptr)
{}
XmlReportV2::~XmlReportV2()
{
delete mXmlReader;
delete mXmlWriter;
}
bool XmlReportV2::create()
{
if (Report::create()) {
mXmlWriter = new QXmlStreamWriter(Report::getFile());
return true;
}
return false;
}
bool XmlReportV2::open()
{
if (Report::open()) {
mXmlReader = new QXmlStreamReader(Report::getFile());
return true;
}
return false;
}
void XmlReportV2::writeHeader()
{
const auto nameAndVersion = Settings::getNameAndVersion(mProductName.toStdString());
const QString name = QString::fromStdString(nameAndVersion.first);
const QString version = nameAndVersion.first.empty() ? CppCheck::version() : QString::fromStdString(nameAndVersion.second);
mXmlWriter->setAutoFormatting(true);
mXmlWriter->writeStartDocument();
mXmlWriter->writeStartElement(ResultElementName);
mXmlWriter->writeAttribute(VersionAttribute, QString::number(2));
mXmlWriter->writeStartElement(CppcheckElementName);
if (!name.isEmpty())
mXmlWriter->writeAttribute(ProductNameAttribute, name);
mXmlWriter->writeAttribute(VersionAttribute, 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>
*/
mXmlWriter->writeStartElement(ErrorElementName);
mXmlWriter->writeAttribute(IdAttribute, error.errorId);
// Don't localize severity so we can read these files
mXmlWriter->writeAttribute(SeverityAttribute, GuiSeverity::toString(error.severity));
const QString summary = XmlReport::quoteMessage(error.summary);
mXmlWriter->writeAttribute(MsgAttribute, summary);
const QString message = XmlReport::quoteMessage(error.message);
mXmlWriter->writeAttribute(VerboseAttribute, message);
if (error.inconclusive)
mXmlWriter->writeAttribute(InconclusiveAttribute, "true");
if (!error.remark.isEmpty())
mXmlWriter->writeAttribute(RemarkAttribute, error.remark);
if (error.cwe > 0)
mXmlWriter->writeAttribute(CWEAttribute, QString::number(error.cwe));
if (error.hash > 0)
mXmlWriter->writeAttribute(HashAttribute, QString::number(error.hash));
if (!error.file0.isEmpty())
mXmlWriter->writeAttribute(IncludedFromFilenameAttribute, quoteMessage(error.file0));
if (!error.sinceDate.isEmpty())
mXmlWriter->writeAttribute(SinceDateAttribute, error.sinceDate);
if (!error.tags.isEmpty())
mXmlWriter->writeAttribute(TagsAttribute, error.tags);
for (int i = error.errorPath.count() - 1; i >= 0; i--) {
mXmlWriter->writeStartElement(LocationElementName);
QString file = QDir::toNativeSeparators(error.errorPath[i].file);
mXmlWriter->writeAttribute(FilenameAttribute, XmlReport::quoteMessage(file));
mXmlWriter->writeAttribute(LineAttribute, QString::number(error.errorPath[i].line));
if (error.errorPath[i].column > 0)
mXmlWriter->writeAttribute(ColumnAttribute, QString::number(error.errorPath[i].column));
if (error.errorPath.count() > 1)
mXmlWriter->writeAttribute(InfoAttribute, XmlReport::quoteMessage(error.errorPath[i].info));
mXmlWriter->writeEndElement();
}
mXmlWriter->writeEndElement();
}
QList<ErrorItem> XmlReportV2::read()
{
QList<ErrorItem> errors;
bool insideResults = false;
if (!mXmlReader) {
qDebug() << "You must Open() the file before reading it!";
return errors;
}
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) {
ErrorItem item = readError(mXmlReader);
errors.append(item);
}
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;
}
ErrorItem XmlReportV2::readError(const QXmlStreamReader *reader)
{
/*
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>
*/
ErrorItem item;
// Read error element from inside errors element
if (mXmlReader->name() == ErrorElementName) {
QXmlStreamAttributes attribs = reader->attributes();
item.errorId = attribs.value(QString(), IdAttribute).toString();
item.severity = GuiSeverity::fromString(attribs.value(QString(), SeverityAttribute).toString());
const QString summary = attribs.value(QString(), MsgAttribute).toString();
item.summary = XmlReport::unquoteMessage(summary);
const QString message = attribs.value(QString(), VerboseAttribute).toString();
item.message = XmlReport::unquoteMessage(message);
if (attribs.hasAttribute(QString(), InconclusiveAttribute))
item.inconclusive = true;
if (attribs.hasAttribute(QString(), RemarkAttribute))
item.remark = attribs.value(QString(), RemarkAttribute).toString();
if (attribs.hasAttribute(QString(), CWEAttribute))
item.cwe = attribs.value(QString(), CWEAttribute).toInt();
if (attribs.hasAttribute(QString(), HashAttribute))
item.hash = attribs.value(QString(), HashAttribute).toULongLong();
if (attribs.hasAttribute(QString(), IncludedFromFilenameAttribute))
item.file0 = attribs.value(QString(), IncludedFromFilenameAttribute).toString();
if (attribs.hasAttribute(QString(), SinceDateAttribute))
item.sinceDate = attribs.value(QString(), SinceDateAttribute).toString();
if (attribs.hasAttribute(QString(), TagsAttribute))
item.tags = attribs.value(QString(), TagsAttribute).toString();
}
bool errorRead = false;
while (!errorRead && !mXmlReader->atEnd()) {
switch (mXmlReader->readNext()) {
case QXmlStreamReader::StartElement:
// Read location element from inside error element
if (mXmlReader->name() == LocationElementName) {
QXmlStreamAttributes attribs = mXmlReader->attributes();
QString file0 = attribs.value(QString(), IncludedFromFilenameAttribute).toString();
if (!file0.isEmpty())
item.file0 = XmlReport::unquoteMessage(file0);
QErrorPathItem loc;
loc.file = XmlReport::unquoteMessage(attribs.value(QString(), FilenameAttribute).toString());
loc.line = attribs.value(QString(), LineAttribute).toString().toUInt();
if (attribs.hasAttribute(QString(), ColumnAttribute))
loc.column = attribs.value(QString(), ColumnAttribute).toString().toInt();
if (attribs.hasAttribute(QString(), InfoAttribute))
loc.info = XmlReport::unquoteMessage(attribs.value(QString(), InfoAttribute).toString());
item.errorPath.push_front(loc);
}
break;
case QXmlStreamReader::EndElement:
if (mXmlReader->name() == ErrorElementName)
errorRead = true;
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;
}
}
if (item.errorPath.size() == 1 && item.errorPath[0].info.isEmpty())
item.errorPath[0].info = item.message;
return item;
}
|