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
|
|
|
|
|
|
|
#include <QDomDocument>
|
|
|
|
#include <QDomNode>
|
|
|
|
#include <QDomNodeList>
|
|
|
|
|
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-08-30 13:41:57 +02:00
|
|
|
static CppcheckLibraryData::Define loadDefine(const QDomElement &defineElement)
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
2015-08-30 13:41:57 +02:00
|
|
|
CppcheckLibraryData::Define define;
|
2015-08-23 16:03:24 +02:00
|
|
|
define.name = defineElement.attribute("name");
|
|
|
|
define.value = defineElement.attribute("value");
|
|
|
|
return define;
|
|
|
|
}
|
|
|
|
|
2015-08-30 13:41:57 +02:00
|
|
|
static CppcheckLibraryData::Function::Arg loadFunctionArg(const QDomElement &functionArgElement)
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
2015-08-30 13:41:57 +02:00
|
|
|
CppcheckLibraryData::Function::Arg arg;
|
2015-08-23 16:03:24 +02:00
|
|
|
if (functionArgElement.attribute("nr") == "any")
|
2015-08-30 13:41:57 +02:00
|
|
|
arg.nr = CppcheckLibraryData::Function::Arg::ANY;
|
2015-08-23 16:03:24 +02:00
|
|
|
else
|
|
|
|
arg.nr = functionArgElement.attribute("nr").toUInt();
|
|
|
|
for (QDomElement childElement = functionArgElement.firstChildElement(); !childElement.isNull(); childElement = childElement.nextSiblingElement()) {
|
|
|
|
if (childElement.tagName() == "not-bool")
|
|
|
|
arg.notbool = true;
|
|
|
|
else if (childElement.tagName() == "not-null")
|
|
|
|
arg.notnull = true;
|
|
|
|
else if (childElement.tagName() == "not-uninit")
|
|
|
|
arg.notuninit = true;
|
|
|
|
else if (childElement.tagName() == "strz")
|
|
|
|
arg.strz = true;
|
|
|
|
else if (childElement.tagName() == "formatstr")
|
|
|
|
arg.formatstr = true;
|
|
|
|
else if (childElement.tagName() == "valid")
|
|
|
|
arg.valid = childElement.text();
|
|
|
|
else if (childElement.tagName() == "minsize") {
|
2015-08-30 13:41:57 +02:00
|
|
|
CppcheckLibraryData::Function::Arg::MinSize minsize;
|
2015-08-30 13:33:43 +02:00
|
|
|
minsize.type = childElement.attribute("type");
|
|
|
|
minsize.arg = childElement.attribute("arg");
|
|
|
|
minsize.arg2 = childElement.attribute("arg2");
|
|
|
|
arg.minsizes.append(minsize);
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
2015-08-30 13:41:57 +02:00
|
|
|
static CppcheckLibraryData::Function loadFunction(const QDomElement &functionElement, 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-08-23 16:03:24 +02:00
|
|
|
function.name = functionElement.attribute("name");
|
|
|
|
for (QDomElement childElement = functionElement.firstChildElement(); !childElement.isNull(); childElement = childElement.nextSiblingElement()) {
|
|
|
|
if (childElement.tagName() == "noreturn")
|
2015-09-03 20:36:26 +02:00
|
|
|
function.noreturn = (childElement.text() == "true") ? CppcheckLibraryData::Function::True : CppcheckLibraryData::Function::False;
|
2015-08-23 16:03:24 +02:00
|
|
|
else if (childElement.tagName() == "pure")
|
|
|
|
function.gccPure = true;
|
|
|
|
else if (childElement.tagName() == "const")
|
|
|
|
function.gccConst = true;
|
|
|
|
else if (childElement.tagName() == "leak-ignore")
|
|
|
|
function.leakignore = true;
|
|
|
|
else if (childElement.tagName() == "use-retval")
|
|
|
|
function.useretval = true;
|
|
|
|
else if (childElement.tagName() == "formatstr") {
|
|
|
|
function.formatstr.scan = childElement.attribute("scan");
|
|
|
|
function.formatstr.secure = childElement.attribute("secure");
|
|
|
|
} else if (childElement.tagName() == "arg") {
|
2015-08-30 13:41:57 +02:00
|
|
|
const CppcheckLibraryData::Function::Arg fa = loadFunctionArg(childElement);
|
2015-08-23 16:03:24 +02:00
|
|
|
function.args.append(fa);
|
|
|
|
} else {
|
|
|
|
int x = 123;
|
|
|
|
x++;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-30 13:41:57 +02:00
|
|
|
static CppcheckLibraryData::MemoryResource loadMemoryResource(const QDomElement &element)
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
2015-08-30 13:41:57 +02:00
|
|
|
CppcheckLibraryData::MemoryResource memoryresource;
|
2015-08-23 16:03:24 +02:00
|
|
|
memoryresource.type = element.tagName();
|
|
|
|
for (QDomElement childElement = element.firstChildElement(); !childElement.isNull(); childElement = childElement.nextSiblingElement()) {
|
|
|
|
if (childElement.tagName() == "alloc") {
|
2015-08-30 13:41:57 +02:00
|
|
|
CppcheckLibraryData::MemoryResource::Alloc alloc;
|
2015-08-23 16:03:24 +02:00
|
|
|
alloc.init = (childElement.attribute("init", "false") == "true");
|
|
|
|
alloc.name = childElement.text();
|
|
|
|
memoryresource.alloc.append(alloc);
|
|
|
|
} else if (childElement.tagName() == "dealloc")
|
|
|
|
memoryresource.dealloc.append(childElement.text());
|
|
|
|
else if (childElement.tagName() == "use")
|
|
|
|
memoryresource.use.append(childElement.text());
|
|
|
|
}
|
|
|
|
return memoryresource;
|
|
|
|
}
|
|
|
|
|
2015-08-30 13:41:57 +02:00
|
|
|
static CppcheckLibraryData::PodType loadPodType(const QDomElement &element)
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
2015-08-30 13:41:57 +02:00
|
|
|
CppcheckLibraryData::PodType podtype;
|
2015-08-23 16:03:24 +02:00
|
|
|
podtype.name = element.attribute("name");
|
|
|
|
podtype.size = element.attribute("size");
|
|
|
|
podtype.sign = element.attribute("sign");
|
|
|
|
return podtype;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-30 13:41:57 +02:00
|
|
|
bool CppcheckLibraryData::open(QIODevice &file)
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
|
|
|
QDomDocument doc;
|
|
|
|
if (!doc.setContent(&file))
|
|
|
|
return false;
|
|
|
|
|
2015-08-29 18:50:08 +02:00
|
|
|
clear();
|
|
|
|
|
2015-08-23 16:03:24 +02:00
|
|
|
QDomElement rootElement = doc.firstChildElement("def");
|
2015-08-29 18:07:59 +02:00
|
|
|
QStringList comments;
|
|
|
|
for (QDomNode n = rootElement.firstChild(); !n.isNull(); n = n.nextSibling()) {
|
|
|
|
if (n.isComment())
|
|
|
|
comments.append(n.toComment().data());
|
|
|
|
else if (n.isElement()) {
|
|
|
|
const QDomElement e = n.toElement();
|
|
|
|
|
|
|
|
if (e.tagName() == "define")
|
|
|
|
defines.append(loadDefine(e));
|
|
|
|
else if (e.tagName() == "function")
|
|
|
|
functions.append(loadFunction(e, comments));
|
|
|
|
else if (e.tagName() == "memory" || e.tagName() == "resource")
|
|
|
|
memoryresource.append(loadMemoryResource(e));
|
|
|
|
else if (e.tagName() == "podtype")
|
|
|
|
podtypes.append(loadPodType(e));
|
|
|
|
|
|
|
|
comments.clear();
|
|
|
|
}
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-30 13:41:57 +02:00
|
|
|
static QDomElement FunctionElement(QDomDocument &doc, const CppcheckLibraryData::Function &function)
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
|
|
|
QDomElement functionElement = doc.createElement("function");
|
|
|
|
functionElement.setAttribute("name", function.name);
|
|
|
|
if (function.useretval)
|
2015-08-29 18:07:59 +02:00
|
|
|
functionElement.appendChild(doc.createElement("use-retval"));
|
|
|
|
if (function.gccConst)
|
|
|
|
functionElement.appendChild(doc.createElement("const"));
|
|
|
|
if (function.gccPure)
|
|
|
|
functionElement.appendChild(doc.createElement("pure"));
|
2015-09-03 20:36:26 +02:00
|
|
|
if (function.noreturn != CppcheckLibraryData::Function::Unknown) {
|
2015-09-03 07:00:43 +02:00
|
|
|
QDomElement e = doc.createElement("noreturn");
|
2015-09-03 20:36:26 +02:00
|
|
|
e.appendChild(doc.createTextNode(function.noreturn == CppcheckLibraryData::Function::True ? "true" : "false"));
|
2015-08-29 18:07:59 +02:00
|
|
|
functionElement.appendChild(e);
|
|
|
|
}
|
2015-09-03 07:00:43 +02:00
|
|
|
if (function.leakignore)
|
|
|
|
functionElement.appendChild(doc.createElement("leak-ignore"));
|
|
|
|
/*
|
|
|
|
if (!function.formatstr.scan.isNull()) {
|
|
|
|
QDomElement e = doc.createElement("formatstr");
|
|
|
|
e.setAttribute("scan", function.formatstr.scan);
|
|
|
|
if (!function.formatstr.secure.isNull())
|
|
|
|
e.setAttribute("secure", function.formatstr.secure);
|
|
|
|
functionElement.appendChild(e);
|
|
|
|
}
|
|
|
|
*/
|
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) {
|
|
|
|
QDomElement e = doc.createElement("formatstr");
|
|
|
|
if (!function.formatstr.scan.isNull())
|
|
|
|
e.setAttribute("scan", function.formatstr.scan);
|
|
|
|
if (!function.formatstr.secure.isNull())
|
|
|
|
e.setAttribute("secure", function.formatstr.secure);
|
|
|
|
functionElement.appendChild(e);
|
|
|
|
}
|
|
|
|
|
2015-08-23 16:03:24 +02:00
|
|
|
QDomElement argElement = doc.createElement("arg");
|
|
|
|
functionElement.appendChild(argElement);
|
2015-08-30 13:41:57 +02:00
|
|
|
if (arg.nr == CppcheckLibraryData::Function::Arg::ANY)
|
2015-08-23 16:03:24 +02:00
|
|
|
argElement.setAttribute("nr", "any");
|
|
|
|
else
|
|
|
|
argElement.setAttribute("nr", arg.nr);
|
2015-09-03 07:00:43 +02:00
|
|
|
if (arg.formatstr)
|
|
|
|
argElement.appendChild(doc.createElement("formatstr"));
|
2015-08-23 16:03:24 +02:00
|
|
|
if (arg.notnull)
|
|
|
|
argElement.appendChild(doc.createElement("not-null"));
|
|
|
|
if (arg.notuninit)
|
|
|
|
argElement.appendChild(doc.createElement("not-uninit"));
|
2015-09-03 07:00:43 +02:00
|
|
|
if (arg.notbool)
|
|
|
|
argElement.appendChild(doc.createElement("not-bool"));
|
2015-08-23 16:03:24 +02:00
|
|
|
if (arg.strz)
|
|
|
|
argElement.appendChild(doc.createElement("strz"));
|
|
|
|
|
|
|
|
if (!arg.valid.isEmpty()) {
|
|
|
|
QDomElement e = doc.createElement("valid");
|
|
|
|
e.appendChild(doc.createTextNode(arg.valid));
|
|
|
|
argElement.appendChild(e);
|
|
|
|
}
|
|
|
|
|
2015-08-30 13:33:43 +02:00
|
|
|
if (!arg.minsizes.isEmpty()) {
|
2015-08-30 13:41:57 +02:00
|
|
|
foreach(const CppcheckLibraryData::Function::Arg::MinSize &minsize, arg.minsizes) {
|
2015-08-30 13:33:43 +02:00
|
|
|
QDomElement e = doc.createElement("minsize");
|
|
|
|
e.setAttribute("type", minsize.type);
|
|
|
|
e.setAttribute("arg", minsize.arg);
|
|
|
|
if (!minsize.arg2.isEmpty())
|
|
|
|
e.setAttribute("arg2", minsize.arg2);
|
|
|
|
argElement.appendChild(e);
|
|
|
|
}
|
2015-08-23 16:03:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return functionElement;
|
|
|
|
}
|
|
|
|
|
2015-08-30 13:41:57 +02:00
|
|
|
static QDomElement MemoryResourceElement(QDomDocument &doc, const CppcheckLibraryData::MemoryResource &mr)
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
|
|
|
QDomElement element = doc.createElement(mr.type);
|
2015-08-30 13:41:57 +02:00
|
|
|
foreach(const CppcheckLibraryData::MemoryResource::Alloc &alloc, mr.alloc) {
|
2015-08-23 16:03:24 +02:00
|
|
|
QDomElement e = doc.createElement("alloc");
|
|
|
|
if (alloc.init)
|
|
|
|
e.setAttribute("init", "true");
|
|
|
|
e.appendChild(doc.createTextNode(alloc.name));
|
|
|
|
element.appendChild(e);
|
|
|
|
}
|
|
|
|
foreach(const QString &dealloc, mr.dealloc) {
|
|
|
|
QDomElement e = doc.createElement("dealloc");
|
|
|
|
e.appendChild(doc.createTextNode(dealloc));
|
|
|
|
element.appendChild(e);
|
|
|
|
}
|
|
|
|
foreach(const QString &use, mr.use) {
|
|
|
|
QDomElement e = doc.createElement("use");
|
|
|
|
e.appendChild(doc.createTextNode(use));
|
|
|
|
element.appendChild(e);
|
|
|
|
}
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-30 13:41:57 +02:00
|
|
|
QString CppcheckLibraryData::toString() const
|
2015-08-23 16:03:24 +02:00
|
|
|
{
|
|
|
|
QDomDocument doc;
|
|
|
|
QDomElement root = doc.createElement("def");
|
|
|
|
doc.appendChild(root);
|
|
|
|
root.setAttribute("format","2");
|
|
|
|
|
|
|
|
foreach(const Define &define, defines) {
|
|
|
|
QDomElement defineElement = doc.createElement("define");
|
|
|
|
defineElement.setAttribute("name", define.name);
|
|
|
|
defineElement.setAttribute("value", define.value);
|
|
|
|
root.appendChild(defineElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(const Function &function, functions) {
|
2015-08-29 18:07:59 +02:00
|
|
|
foreach(const QString &comment, function.comments) {
|
|
|
|
root.appendChild(doc.createComment(comment));
|
|
|
|
}
|
2015-08-23 16:03:24 +02:00
|
|
|
root.appendChild(FunctionElement(doc, function));
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(const MemoryResource &mr, memoryresource) {
|
|
|
|
root.appendChild(MemoryResourceElement(doc, mr));
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(const PodType &podtype, podtypes) {
|
|
|
|
QDomElement podtypeElement = doc.createElement("podtype");
|
|
|
|
podtypeElement.setAttribute("name", podtype.name);
|
2015-09-03 07:00:43 +02:00
|
|
|
if (!podtype.size.isEmpty())
|
|
|
|
podtypeElement.setAttribute("size", podtype.size);
|
|
|
|
if (!podtype.sign.isEmpty())
|
|
|
|
podtypeElement.setAttribute("sign", podtype.sign);
|
2015-08-23 16:03:24 +02:00
|
|
|
root.appendChild(podtypeElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
return doc.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
|