2015-08-23 16:03:24 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
|
|
|
* Copyright (C) 2007-2015 Daniel Marjamäki and 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/>.
|
|
|
|
*/
|
|
|
|
|
2015-08-30 13:41:57 +02:00
|
|
|
#include "cppchecklibrarydata.h"
|
2015-08-23 16:03:24 +02:00
|
|
|
|
2015-09-05 11:37:55 +02:00
|
|
|
#include <QXmlStreamReader>
|
|
|
|
#include <QXmlStreamWriter>
|
2015-08-23 16:03:24 +02:00
|
|
|
|
2015-08-30 13:41:57 +02:00
|
|
|
const unsigned int CppcheckLibraryData::Function::Arg::ANY = ~0U;
|
2015-08-23 16:03:24 +02:00
|
|
|
|
2015-08-30 13:41:57 +02:00
|
|
|
CppcheckLibraryData::CppcheckLibraryData()
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-09-05 11:37:55 +02:00
|
|
|
static CppcheckLibraryData::Define loadDefine(const QXmlStreamReader &xmlReader)
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
2015-08-30 13:41:57 +02:00
|
|
|
CppcheckLibraryData::Define define;
|
2015-09-05 11:37:55 +02:00
|
|
|
define.name = xmlReader.attributes().value("name").toString();
|
|
|
|
define.value = xmlReader.attributes().value("value").toString();
|
2015-08-23 16:03:24 +02:00
|
|
|
return define;
|
|
|
|
}
|
|
|
|
|
2015-09-05 11:37:55 +02:00
|
|
|
static CppcheckLibraryData::Function::Arg loadFunctionArg(QXmlStreamReader &xmlReader)
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
2015-08-30 13:41:57 +02:00
|
|
|
CppcheckLibraryData::Function::Arg arg;
|
2015-09-05 11:37:55 +02:00
|
|
|
QString argnr = xmlReader.attributes().value("nr").toString();
|
|
|
|
if (argnr == "any")
|
2015-08-30 13:41:57 +02:00
|
|
|
arg.nr = CppcheckLibraryData::Function::Arg::ANY;
|
2015-08-23 16:03:24 +02:00
|
|
|
else
|
2015-09-05 11:37:55 +02:00
|
|
|
arg.nr = argnr.toUInt();
|
|
|
|
|
|
|
|
QXmlStreamReader::TokenType type;
|
|
|
|
while ((type = xmlReader.readNext()) != QXmlStreamReader::EndElement ||
|
|
|
|
xmlReader.name().toString() != "arg") {
|
|
|
|
if (type != QXmlStreamReader::StartElement)
|
|
|
|
continue;
|
|
|
|
const QString elementName = xmlReader.name().toString();
|
|
|
|
if (elementName == "not-bool")
|
2015-08-23 16:03:24 +02:00
|
|
|
arg.notbool = true;
|
2015-09-05 11:37:55 +02:00
|
|
|
else if (elementName == "not-null")
|
2015-08-23 16:03:24 +02:00
|
|
|
arg.notnull = true;
|
2015-09-05 11:37:55 +02:00
|
|
|
else if (elementName == "not-uninit")
|
2015-08-23 16:03:24 +02:00
|
|
|
arg.notuninit = true;
|
2015-09-05 11:37:55 +02:00
|
|
|
else if (elementName == "strz")
|
2015-08-23 16:03:24 +02:00
|
|
|
arg.strz = true;
|
2015-09-05 11:37:55 +02:00
|
|
|
else if (elementName == "formatstr")
|
2015-08-23 16:03:24 +02:00
|
|
|
arg.formatstr = true;
|
2015-09-05 11:37:55 +02:00
|
|
|
else if (elementName == "valid")
|
|
|
|
arg.valid = xmlReader.readElementText();
|
|
|
|
else if (elementName == "minsize") {
|
2015-08-30 13:41:57 +02:00
|
|
|
CppcheckLibraryData::Function::Arg::MinSize minsize;
|
2015-09-05 11:37:55 +02:00
|
|
|
minsize.type = xmlReader.attributes().value("type").toString();
|
|
|
|
minsize.arg = xmlReader.attributes().value("arg").toString();
|
|
|
|
minsize.arg2 = xmlReader.attributes().value("arg2").toString();
|
2015-08-30 13:33:43 +02:00
|
|
|
arg.minsizes.append(minsize);
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
2015-09-05 11:37:55 +02:00
|
|
|
static CppcheckLibraryData::Function loadFunction(QXmlStreamReader &xmlReader, const QStringList &comments)
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
2015-08-30 13:41:57 +02:00
|
|
|
CppcheckLibraryData::Function function;
|
2015-08-29 18:07:59 +02:00
|
|
|
function.comments = comments;
|
2015-09-05 11:37:55 +02:00
|
|
|
function.name = xmlReader.attributes().value("name").toString();
|
|
|
|
QXmlStreamReader::TokenType type;
|
|
|
|
while ((type = xmlReader.readNext()) != QXmlStreamReader::EndElement ||
|
|
|
|
xmlReader.name().toString() != "function") {
|
|
|
|
if (type != QXmlStreamReader::StartElement)
|
|
|
|
continue;
|
|
|
|
const QString elementName = xmlReader.name().toString();
|
|
|
|
if (elementName == "noreturn")
|
|
|
|
function.noreturn = (xmlReader.readElementText() == "true") ? CppcheckLibraryData::Function::True : CppcheckLibraryData::Function::False;
|
|
|
|
else if (elementName == "pure")
|
2015-08-23 16:03:24 +02:00
|
|
|
function.gccPure = true;
|
2015-09-05 11:37:55 +02:00
|
|
|
else if (elementName == "const")
|
2015-08-23 16:03:24 +02:00
|
|
|
function.gccConst = true;
|
2015-09-05 11:37:55 +02:00
|
|
|
else if (elementName == "leak-ignore")
|
2015-08-23 16:03:24 +02:00
|
|
|
function.leakignore = true;
|
2015-09-05 11:37:55 +02:00
|
|
|
else if (elementName == "use-retval")
|
2015-08-23 16:03:24 +02:00
|
|
|
function.useretval = true;
|
2015-09-05 11:37:55 +02:00
|
|
|
else if (elementName == "formatstr") {
|
|
|
|
function.formatstr.scan = xmlReader.attributes().value("scan").toString();
|
|
|
|
function.formatstr.secure = xmlReader.attributes().value("secure").toString();
|
|
|
|
} else if (elementName == "arg")
|
|
|
|
function.args.append(loadFunctionArg(xmlReader));
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-05 11:37:55 +02:00
|
|
|
static CppcheckLibraryData::MemoryResource loadMemoryResource(QXmlStreamReader &xmlReader)
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
2015-08-30 13:41:57 +02:00
|
|
|
CppcheckLibraryData::MemoryResource memoryresource;
|
2015-09-05 11:37:55 +02:00
|
|
|
memoryresource.type = xmlReader.name().toString();
|
|
|
|
QXmlStreamReader::TokenType type;
|
|
|
|
while ((type = xmlReader.readNext()) != QXmlStreamReader::EndElement ||
|
|
|
|
xmlReader.name().toString() != memoryresource.type) {
|
|
|
|
if (type != QXmlStreamReader::StartElement)
|
|
|
|
continue;
|
|
|
|
const QString elementName = xmlReader.name().toString();
|
|
|
|
if (elementName == "alloc") {
|
2015-08-30 13:41:57 +02:00
|
|
|
CppcheckLibraryData::MemoryResource::Alloc alloc;
|
2015-09-05 11:37:55 +02:00
|
|
|
alloc.init = (xmlReader.attributes().value("init").toString() == "true");
|
|
|
|
alloc.name = xmlReader.readElementText();
|
2015-08-23 16:03:24 +02:00
|
|
|
memoryresource.alloc.append(alloc);
|
2015-09-05 11:37:55 +02:00
|
|
|
} else if (elementName == "dealloc")
|
|
|
|
memoryresource.dealloc.append(xmlReader.readElementText());
|
|
|
|
else if (elementName == "use")
|
|
|
|
memoryresource.use.append(xmlReader.readElementText());
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
|
|
|
return memoryresource;
|
|
|
|
}
|
|
|
|
|
2015-09-05 11:37:55 +02:00
|
|
|
static CppcheckLibraryData::PodType loadPodType(const QXmlStreamReader &xmlReader)
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
2015-08-30 13:41:57 +02:00
|
|
|
CppcheckLibraryData::PodType podtype;
|
2015-09-05 11:37:55 +02:00
|
|
|
podtype.name = xmlReader.attributes().value("name").toString();
|
|
|
|
podtype.size = xmlReader.attributes().value("size").toString();
|
|
|
|
podtype.sign = xmlReader.attributes().value("sign").toString();
|
2015-08-23 16:03:24 +02:00
|
|
|
return podtype;
|
|
|
|
}
|
|
|
|
|
2015-08-30 13:41:57 +02:00
|
|
|
bool CppcheckLibraryData::open(QIODevice &file)
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
2015-08-29 18:50:08 +02:00
|
|
|
clear();
|
2015-08-29 18:07:59 +02:00
|
|
|
QStringList comments;
|
2015-09-05 11:37:55 +02:00
|
|
|
QXmlStreamReader xmlReader(&file);
|
|
|
|
while (!xmlReader.atEnd()) {
|
|
|
|
const QXmlStreamReader::TokenType t = xmlReader.readNext();
|
|
|
|
switch (t) {
|
|
|
|
case QXmlStreamReader::Comment:
|
|
|
|
comments.append(xmlReader.text().toString());
|
|
|
|
break;
|
|
|
|
case QXmlStreamReader::StartElement:
|
|
|
|
if (xmlReader.name() == "define")
|
|
|
|
defines.append(loadDefine(xmlReader));
|
|
|
|
else if (xmlReader.name() == "function")
|
|
|
|
functions.append(loadFunction(xmlReader, comments));
|
|
|
|
else if (xmlReader.name() == "memory" || xmlReader.name() == "resource")
|
|
|
|
memoryresource.append(loadMemoryResource(xmlReader));
|
|
|
|
else if (xmlReader.name() == "podtype")
|
|
|
|
podtypes.append(loadPodType(xmlReader));
|
2015-08-29 18:07:59 +02:00
|
|
|
comments.clear();
|
2015-09-05 11:37:55 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2015-08-29 18:07:59 +02:00
|
|
|
}
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-05 11:37:55 +02:00
|
|
|
static void writeFunction(QXmlStreamWriter &xmlWriter, const CppcheckLibraryData::Function &function)
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
2015-09-05 11:37:55 +02:00
|
|
|
foreach(const QString &comment, function.comments) {
|
|
|
|
xmlWriter.writeComment(comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlWriter.writeStartElement("function");
|
|
|
|
xmlWriter.writeAttribute("name", function.name);
|
|
|
|
|
2015-08-23 16:03:24 +02:00
|
|
|
if (function.useretval)
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeEmptyElement("use-retval");
|
2015-08-29 18:07:59 +02:00
|
|
|
if (function.gccConst)
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeEmptyElement("const");
|
2015-08-29 18:07:59 +02:00
|
|
|
if (function.gccPure)
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeEmptyElement("pure");
|
|
|
|
if (function.noreturn != CppcheckLibraryData::Function::Unknown)
|
|
|
|
xmlWriter.writeTextElement("noreturn", (function.noreturn == CppcheckLibraryData::Function::True) ? "true" : "false");
|
2015-09-03 07:00:43 +02:00
|
|
|
if (function.leakignore)
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeEmptyElement("leak-ignore");
|
2015-08-23 16:03:24 +02:00
|
|
|
// Argument info..
|
2015-08-30 13:41:57 +02:00
|
|
|
foreach(const CppcheckLibraryData::Function::Arg &arg, function.args) {
|
2015-09-03 07:00:43 +02:00
|
|
|
if (arg.formatstr) {
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeStartElement("formatstr");
|
2015-09-03 07:00:43 +02:00
|
|
|
if (!function.formatstr.scan.isNull())
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeAttribute("scan", function.formatstr.scan);
|
2015-09-03 07:00:43 +02:00
|
|
|
if (!function.formatstr.secure.isNull())
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeAttribute("secure", function.formatstr.secure);
|
|
|
|
xmlWriter.writeEndElement();
|
2015-09-03 07:00:43 +02:00
|
|
|
}
|
|
|
|
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeStartElement("arg");
|
2015-08-30 13:41:57 +02:00
|
|
|
if (arg.nr == CppcheckLibraryData::Function::Arg::ANY)
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeAttribute("nr", "any");
|
2015-08-23 16:03:24 +02:00
|
|
|
else
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeAttribute("nr", QString::number(arg.nr));
|
2015-09-03 07:00:43 +02:00
|
|
|
if (arg.formatstr)
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeEmptyElement("formatstr");
|
2015-08-23 16:03:24 +02:00
|
|
|
if (arg.notnull)
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeEmptyElement("not-null");
|
2015-08-23 16:03:24 +02:00
|
|
|
if (arg.notuninit)
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeEmptyElement("not-uninit");
|
2015-09-03 07:00:43 +02:00
|
|
|
if (arg.notbool)
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeEmptyElement("not-bool");
|
2015-08-23 16:03:24 +02:00
|
|
|
if (arg.strz)
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeEmptyElement("strz");
|
|
|
|
|
|
|
|
if (!arg.valid.isEmpty())
|
|
|
|
xmlWriter.writeTextElement("valid",arg.valid);
|
|
|
|
|
|
|
|
foreach(const CppcheckLibraryData::Function::Arg::MinSize &minsize, arg.minsizes) {
|
|
|
|
xmlWriter.writeStartElement("minsize");
|
|
|
|
xmlWriter.writeAttribute("type", minsize.type);
|
|
|
|
xmlWriter.writeAttribute("arg", minsize.arg);
|
|
|
|
if (!minsize.arg2.isEmpty())
|
|
|
|
xmlWriter.writeAttribute("arg2", minsize.arg2);
|
|
|
|
xmlWriter.writeEndElement();
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
|
|
|
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeEndElement();
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeEndElement();
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
|
|
|
|
2015-09-05 11:37:55 +02:00
|
|
|
static void writeMemoryResource(QXmlStreamWriter &xmlWriter, const CppcheckLibraryData::MemoryResource &mr)
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeStartElement(mr.type);
|
2015-08-30 13:41:57 +02:00
|
|
|
foreach(const CppcheckLibraryData::MemoryResource::Alloc &alloc, mr.alloc) {
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeStartElement("alloc");
|
2015-08-23 16:03:24 +02:00
|
|
|
if (alloc.init)
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeAttribute("init", "true");
|
|
|
|
xmlWriter.writeCharacters(alloc.name);
|
|
|
|
xmlWriter.writeEndElement();
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
|
|
|
foreach(const QString &dealloc, mr.dealloc) {
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeTextElement("dealloc", dealloc);
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
|
|
|
foreach(const QString &use, mr.use) {
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeTextElement("use", use);
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeEndElement();
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-30 13:41:57 +02:00
|
|
|
QString CppcheckLibraryData::toString() const
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
2015-09-05 11:37:55 +02:00
|
|
|
QString outputString;
|
|
|
|
QXmlStreamWriter xmlWriter(&outputString);
|
|
|
|
xmlWriter.setAutoFormatting(true);
|
|
|
|
xmlWriter.writeStartDocument("1.0");
|
|
|
|
xmlWriter.writeStartElement("def");
|
|
|
|
xmlWriter.writeAttribute("format","2");
|
2015-08-23 16:03:24 +02:00
|
|
|
|
|
|
|
foreach(const Define &define, defines) {
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeStartElement("define");
|
|
|
|
xmlWriter.writeAttribute("name", define.name);
|
|
|
|
xmlWriter.writeAttribute("value", define.value);
|
|
|
|
xmlWriter.writeEndElement();
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach(const Function &function, functions) {
|
2015-09-05 11:37:55 +02:00
|
|
|
writeFunction(xmlWriter, function);
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach(const MemoryResource &mr, memoryresource) {
|
2015-09-05 11:37:55 +02:00
|
|
|
writeMemoryResource(xmlWriter, mr);
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach(const PodType &podtype, podtypes) {
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeStartElement("podtype");
|
|
|
|
xmlWriter.writeAttribute("name", podtype.name);
|
2015-09-03 07:00:43 +02:00
|
|
|
if (!podtype.size.isEmpty())
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeAttribute("size", podtype.size);
|
2015-09-03 07:00:43 +02:00
|
|
|
if (!podtype.sign.isEmpty())
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeAttribute("sign", podtype.sign);
|
|
|
|
xmlWriter.writeEndElement();
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
|
|
|
|
2015-09-05 11:37:55 +02:00
|
|
|
xmlWriter.writeEndElement();
|
2015-08-23 16:03:24 +02:00
|
|
|
|
2015-09-05 11:37:55 +02:00
|
|
|
return outputString;
|
|
|
|
}
|