cppcheck/gui/projectfile.cpp

1168 lines
40 KiB
C++
Raw Normal View History

/*
* Cppcheck - A tool for static C/C++ code analysis
2020-05-10 11:16:32 +02:00
* Copyright (C) 2007-2020 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 <QObject>
#include <QString>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QFile>
#include <QDir>
#include "projectfile.h"
#include "common.h"
#include "importproject.h"
#include "settings.h"
2020-07-15 13:03:07 +02:00
ProjectFile *ProjectFile::mActiveProject;
ProjectFile::ProjectFile(QObject *parent) :
2010-04-15 20:08:51 +02:00
QObject(parent)
{
clear();
}
ProjectFile::ProjectFile(const QString &filename, QObject *parent) :
2010-04-15 20:08:51 +02:00
QObject(parent),
mFilename(filename)
{
clear();
2017-07-31 12:02:51 +02:00
read();
}
void ProjectFile::clear()
{
const Settings settings;
2020-02-02 10:45:42 +01:00
clangParser = false;
bugHunting = false;
mRootPath.clear();
mBuildDir.clear();
mImportProject.clear();
mAnalyzeAllVsConfigs = true;
mIncludeDirs.clear();
mDefines.clear();
mUndefines.clear();
mPaths.clear();
mExcludedPaths.clear();
mFunctionContracts.clear();
2020-08-22 11:37:44 +02:00
mVariableContracts.clear();
mLibraries.clear();
mPlatform.clear();
mSuppressions.clear();
mAddons.clear();
mClangAnalyzer = mClangTidy = false;
mAnalyzeAllVsConfigs = false;
mCheckHeaders = true;
2020-11-23 18:29:08 +01:00
mCheckUnusedTemplates = true;
mMaxCtuDepth = settings.maxCtuDepth;
mMaxTemplateRecursion = settings.maxTemplateRecursion;
mCheckUnknownFunctionReturn.clear();
safeChecks.clear();
mVsConfigurations.clear();
mTags.clear();
mWarningTags.clear();
}
2017-07-28 05:08:59 +02:00
bool ProjectFile::read(const QString &filename)
{
if (!filename.isEmpty())
mFilename = filename;
QFile file(mFilename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
clear();
QXmlStreamReader xmlReader(&file);
bool insideProject = false;
bool projectTagFound = false;
2011-10-13 20:53:06 +02:00
while (!xmlReader.atEnd()) {
switch (xmlReader.readNext()) {
case QXmlStreamReader::StartElement:
if (xmlReader.name() == CppcheckXml::ProjectElementName) {
insideProject = true;
projectTagFound = true;
break;
}
if (!insideProject)
break;
// Read root path from inside project element
if (xmlReader.name() == CppcheckXml::RootPathName)
2017-07-28 05:08:59 +02:00
readRootPath(xmlReader);
2016-11-19 20:38:50 +01:00
// Read root path from inside project element
if (xmlReader.name() == CppcheckXml::BuildDirElementName)
2017-07-28 05:08:59 +02:00
readBuildDir(xmlReader);
2016-11-19 20:38:50 +01:00
// Find paths to check from inside project element
if (xmlReader.name() == CppcheckXml::PathsElementName)
2017-07-28 05:08:59 +02:00
readCheckPaths(xmlReader);
if (xmlReader.name() == CppcheckXml::ImportProjectElementName)
2017-07-28 05:08:59 +02:00
readImportProject(xmlReader);
2016-08-18 21:58:50 +02:00
if (xmlReader.name() == CppcheckXml::AnalyzeAllVsConfigsElementName)
mAnalyzeAllVsConfigs = readBool(xmlReader);
2020-02-02 10:45:42 +01:00
if (xmlReader.name() == CppcheckXml::Parser)
clangParser = true;
if (xmlReader.name() == CppcheckXml::BugHunting)
bugHunting = true;
if (xmlReader.name() == CppcheckXml::CheckHeadersElementName)
mCheckHeaders = readBool(xmlReader);
if (xmlReader.name() == CppcheckXml::CheckUnusedTemplatesElementName)
mCheckUnusedTemplates = readBool(xmlReader);
// Find include directory from inside project element
if (xmlReader.name() == CppcheckXml::IncludeDirElementName)
2017-07-28 05:08:59 +02:00
readIncludeDirs(xmlReader);
// Find preprocessor define from inside project element
if (xmlReader.name() == CppcheckXml::DefinesElementName)
2017-07-28 05:08:59 +02:00
readDefines(xmlReader);
// Find preprocessor define from inside project element
if (xmlReader.name() == CppcheckXml::UndefinesElementName)
readStringList(mUndefines, xmlReader, CppcheckXml::UndefineName);
// Find exclude list from inside project element
if (xmlReader.name() == CppcheckXml::ExcludeElementName)
2017-07-28 05:08:59 +02:00
readExcludes(xmlReader);
// Find ignore list from inside project element
// These are read for compatibility
if (xmlReader.name() == CppcheckXml::IgnoreElementName)
2017-07-28 05:08:59 +02:00
readExcludes(xmlReader);
// Function contracts
if (xmlReader.name() == CppcheckXml::FunctionContracts)
readFunctionContracts(xmlReader);
2020-08-22 11:37:44 +02:00
// Variable constraints
if (xmlReader.name() == CppcheckXml::VariableContractsElementName)
readVariableContracts(xmlReader);
2013-12-30 22:32:50 +01:00
// Find libraries list from inside project element
if (xmlReader.name() == CppcheckXml::LibrariesElementName)
readStringList(mLibraries, xmlReader, CppcheckXml::LibraryElementName);
2013-12-30 22:32:50 +01:00
if (xmlReader.name() == CppcheckXml::PlatformElementName)
readPlatform(xmlReader);
2013-12-30 22:32:50 +01:00
// Find suppressions list from inside project element
if (xmlReader.name() == CppcheckXml::SuppressionsElementName)
readSuppressions(xmlReader);
2013-12-29 18:06:31 +01:00
// Unknown function return values
if (xmlReader.name() == CppcheckXml::CheckUnknownFunctionReturn)
readStringList(mCheckUnknownFunctionReturn, xmlReader, CppcheckXml::Name);
// check all function parameter values
if (xmlReader.name() == Settings::SafeChecks::XmlRootName)
safeChecks.loadFromXml(xmlReader);
// Addons
if (xmlReader.name() == CppcheckXml::AddonsElementName)
readStringList(mAddons, xmlReader, CppcheckXml::AddonElementName);
// Tools
if (xmlReader.name() == CppcheckXml::ToolsElementName) {
QStringList tools;
readStringList(tools, xmlReader, CppcheckXml::ToolElementName);
mClangAnalyzer = tools.contains(CLANG_ANALYZER);
mClangTidy = tools.contains(CLANG_TIDY);
}
if (xmlReader.name() == CppcheckXml::TagsElementName)
readStringList(mTags, xmlReader, CppcheckXml::TagElementName);
if (xmlReader.name() == CppcheckXml::TagWarningsElementName)
readTagWarnings(xmlReader, xmlReader.attributes().value(QString(), CppcheckXml::TagAttributeName).toString());
if (xmlReader.name() == CppcheckXml::MaxCtuDepthElementName)
mMaxCtuDepth = readInt(xmlReader, mMaxCtuDepth);
if (xmlReader.name() == CppcheckXml::MaxTemplateRecursionElementName)
mMaxTemplateRecursion = readInt(xmlReader, mMaxTemplateRecursion);
// VSConfiguration
if (xmlReader.name() == CppcheckXml::VSConfigurationElementName)
readVsConfigurations(xmlReader);
break;
case QXmlStreamReader::EndElement:
if (xmlReader.name() == CppcheckXml::ProjectElementName)
insideProject = 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;
}
}
file.close();
2017-07-28 05:08:59 +02:00
return projectTagFound;
}
2017-07-28 05:08:59 +02:00
void ProjectFile::readRootPath(QXmlStreamReader &reader)
{
QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value(QString(), CppcheckXml::RootPathNameAttrib).toString();
if (!name.isEmpty())
mRootPath = name;
}
2017-07-28 05:08:59 +02:00
void ProjectFile::readBuildDir(QXmlStreamReader &reader)
2016-11-19 20:38:50 +01:00
{
mBuildDir.clear();
do {
const QXmlStreamReader::TokenType type = reader.readNext();
switch (type) {
case QXmlStreamReader::Characters:
mBuildDir = reader.text().toString();
case QXmlStreamReader::EndElement:
return;
// Not handled
case QXmlStreamReader::StartElement:
case QXmlStreamReader::NoToken:
case QXmlStreamReader::Invalid:
case QXmlStreamReader::StartDocument:
case QXmlStreamReader::EndDocument:
case QXmlStreamReader::Comment:
case QXmlStreamReader::DTD:
case QXmlStreamReader::EntityReference:
case QXmlStreamReader::ProcessingInstruction:
break;
}
} while (true);
2016-11-19 20:38:50 +01:00
}
2017-07-28 05:08:59 +02:00
void ProjectFile::readImportProject(QXmlStreamReader &reader)
2016-08-18 21:58:50 +02:00
{
mImportProject.clear();
do {
const QXmlStreamReader::TokenType type = reader.readNext();
switch (type) {
case QXmlStreamReader::Characters:
mImportProject = reader.text().toString();
case QXmlStreamReader::EndElement:
return;
// Not handled
case QXmlStreamReader::StartElement:
case QXmlStreamReader::NoToken:
case QXmlStreamReader::Invalid:
case QXmlStreamReader::StartDocument:
case QXmlStreamReader::EndDocument:
case QXmlStreamReader::Comment:
case QXmlStreamReader::DTD:
case QXmlStreamReader::EntityReference:
case QXmlStreamReader::ProcessingInstruction:
break;
}
} while (true);
2016-08-18 21:58:50 +02:00
}
bool ProjectFile::readBool(QXmlStreamReader &reader)
{
bool ret = false;
do {
const QXmlStreamReader::TokenType type = reader.readNext();
switch (type) {
case QXmlStreamReader::Characters:
ret = (reader.text().toString() == "true");
case QXmlStreamReader::EndElement:
return ret;
// Not handled
case QXmlStreamReader::StartElement:
case QXmlStreamReader::NoToken:
case QXmlStreamReader::Invalid:
case QXmlStreamReader::StartDocument:
case QXmlStreamReader::EndDocument:
case QXmlStreamReader::Comment:
case QXmlStreamReader::DTD:
case QXmlStreamReader::EntityReference:
case QXmlStreamReader::ProcessingInstruction:
break;
}
} while (true);
}
int ProjectFile::readInt(QXmlStreamReader &reader, int defaultValue)
{
int ret = defaultValue;
do {
const QXmlStreamReader::TokenType type = reader.readNext();
switch (type) {
case QXmlStreamReader::Characters:
ret = reader.text().toString().toInt();
case QXmlStreamReader::EndElement:
return ret;
// Not handled
case QXmlStreamReader::StartElement:
case QXmlStreamReader::NoToken:
case QXmlStreamReader::Invalid:
case QXmlStreamReader::StartDocument:
case QXmlStreamReader::EndDocument:
case QXmlStreamReader::Comment:
case QXmlStreamReader::DTD:
case QXmlStreamReader::EntityReference:
case QXmlStreamReader::ProcessingInstruction:
break;
}
} while (true);
}
2017-07-28 05:08:59 +02:00
void ProjectFile::readIncludeDirs(QXmlStreamReader &reader)
{
QXmlStreamReader::TokenType type;
bool allRead = false;
2011-10-13 20:53:06 +02:00
do {
type = reader.readNext();
2011-10-13 20:53:06 +02:00
switch (type) {
case QXmlStreamReader::StartElement:
// Read dir-elements
if (reader.name().toString() == CppcheckXml::DirElementName) {
QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value(QString(), CppcheckXml::DirNameAttrib).toString();
if (!name.isEmpty())
mIncludeDirs << name;
}
break;
case QXmlStreamReader::EndElement:
if (reader.name().toString() == CppcheckXml::IncludeDirElementName)
allRead = 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;
}
2011-10-13 20:53:06 +02:00
} while (!allRead);
}
2017-07-28 05:08:59 +02:00
void ProjectFile::readDefines(QXmlStreamReader &reader)
{
QXmlStreamReader::TokenType type;
bool allRead = false;
2011-10-13 20:53:06 +02:00
do {
type = reader.readNext();
2011-10-13 20:53:06 +02:00
switch (type) {
case QXmlStreamReader::StartElement:
// Read define-elements
if (reader.name().toString() == CppcheckXml::DefineName) {
QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value(QString(), CppcheckXml::DefineNameAttrib).toString();
if (!name.isEmpty())
mDefines << name;
}
break;
case QXmlStreamReader::EndElement:
if (reader.name().toString() == CppcheckXml::DefinesElementName)
allRead = 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;
}
2011-10-13 20:53:06 +02:00
} while (!allRead);
}
2017-07-28 05:08:59 +02:00
void ProjectFile::readCheckPaths(QXmlStreamReader &reader)
{
QXmlStreamReader::TokenType type;
bool allRead = false;
2011-10-13 20:53:06 +02:00
do {
type = reader.readNext();
2011-10-13 20:53:06 +02:00
switch (type) {
case QXmlStreamReader::StartElement:
// Read dir-elements
if (reader.name().toString() == CppcheckXml::PathName) {
QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value(QString(), CppcheckXml::PathNameAttrib).toString();
if (!name.isEmpty())
mPaths << name;
}
break;
case QXmlStreamReader::EndElement:
if (reader.name().toString() == CppcheckXml::PathsElementName)
allRead = 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;
}
2011-10-13 20:53:06 +02:00
} while (!allRead);
}
2017-07-28 05:08:59 +02:00
void ProjectFile::readExcludes(QXmlStreamReader &reader)
{
QXmlStreamReader::TokenType type;
bool allRead = false;
2011-10-13 20:53:06 +02:00
do {
type = reader.readNext();
2011-10-13 20:53:06 +02:00
switch (type) {
case QXmlStreamReader::StartElement:
// Read exclude-elements
if (reader.name().toString() == CppcheckXml::ExcludePathName) {
QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value(QString(), CppcheckXml::ExcludePathNameAttrib).toString();
if (!name.isEmpty())
mExcludedPaths << name;
}
// Read ignore-elements - deprecated but support reading them
else if (reader.name().toString() == CppcheckXml::IgnorePathName) {
QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value(QString(), CppcheckXml::IgnorePathNameAttrib).toString();
if (!name.isEmpty())
mExcludedPaths << name;
}
break;
case QXmlStreamReader::EndElement:
if (reader.name().toString() == CppcheckXml::IgnoreElementName)
allRead = true;
if (reader.name().toString() == CppcheckXml::ExcludeElementName)
allRead = 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;
}
2011-10-13 20:53:06 +02:00
} while (!allRead);
}
void ProjectFile::readFunctionContracts(QXmlStreamReader &reader)
{
QXmlStreamReader::TokenType type;
bool allRead = false;
do {
type = reader.readNext();
switch (type) {
case QXmlStreamReader::StartElement:
if (reader.name().toString() == CppcheckXml::FunctionContract) {
QXmlStreamAttributes attribs = reader.attributes();
QString function = attribs.value(QString(), CppcheckXml::ContractFunction).toString();
QString expects = attribs.value(QString(), CppcheckXml::ContractExpects).toString();
if (!function.isEmpty() && !expects.isEmpty())
mFunctionContracts[function.toStdString()] = expects.toStdString();
}
break;
case QXmlStreamReader::EndElement:
if (reader.name().toString() == CppcheckXml::FunctionContracts)
allRead = 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;
}
} while (!allRead);
}
2020-08-22 11:37:44 +02:00
void ProjectFile::readVariableContracts(QXmlStreamReader &reader)
{
QXmlStreamReader::TokenType type;
while (true) {
type = reader.readNext();
switch (type) {
case QXmlStreamReader::StartElement:
if (reader.name().toString() == CppcheckXml::VariableContractItemElementName) {
QXmlStreamAttributes attribs = reader.attributes();
QString varname = attribs.value(QString(), CppcheckXml::VariableContractVarName).toString();
QString minValue = attribs.value(QString(), CppcheckXml::VariableContractMin).toString();
QString maxValue = attribs.value(QString(), CppcheckXml::VariableContractMax).toString();
setVariableContracts(varname, minValue, maxValue);
}
break;
case QXmlStreamReader::EndElement:
if (reader.name().toString() == CppcheckXml::VariableContractsElementName)
return;
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;
}
}
}
void ProjectFile::readVsConfigurations(QXmlStreamReader &reader)
{
QXmlStreamReader::TokenType type;
do {
type = reader.readNext();
switch (type) {
case QXmlStreamReader::StartElement:
// Read library-elements
if (reader.name().toString() == CppcheckXml::VSConfigurationName) {
QString config;
type = reader.readNext();
if (type == QXmlStreamReader::Characters) {
config = reader.text().toString();
}
mVsConfigurations << config;
}
break;
case QXmlStreamReader::EndElement:
if (reader.name().toString() != CppcheckXml::VSConfigurationName)
return;
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;
}
} while (true);
}
void ProjectFile::readPlatform(QXmlStreamReader &reader)
{
do {
const QXmlStreamReader::TokenType type = reader.readNext();
switch (type) {
case QXmlStreamReader::Characters:
mPlatform = reader.text().toString();
case QXmlStreamReader::EndElement:
return;
// Not handled
case QXmlStreamReader::StartElement:
case QXmlStreamReader::NoToken:
case QXmlStreamReader::Invalid:
case QXmlStreamReader::StartDocument:
case QXmlStreamReader::EndDocument:
case QXmlStreamReader::Comment:
case QXmlStreamReader::DTD:
case QXmlStreamReader::EntityReference:
case QXmlStreamReader::ProcessingInstruction:
break;
}
} while (true);
}
2013-12-29 18:06:31 +01:00
void ProjectFile::readSuppressions(QXmlStreamReader &reader)
{
QXmlStreamReader::TokenType type;
do {
type = reader.readNext();
switch (type) {
case QXmlStreamReader::StartElement:
// Read library-elements
if (reader.name().toString() == CppcheckXml::SuppressionElementName) {
Suppressions::Suppression suppression;
if (reader.attributes().hasAttribute(QString(),"fileName"))
suppression.fileName = reader.attributes().value(QString(),"fileName").toString().toStdString();
if (reader.attributes().hasAttribute(QString(),"lineNumber"))
suppression.lineNumber = reader.attributes().value(QString(),"lineNumber").toInt();
if (reader.attributes().hasAttribute(QString(),"symbolName"))
suppression.symbolName = reader.attributes().value(QString(),"symbolName").toString().toStdString();
2020-07-21 11:27:03 +02:00
if (reader.attributes().hasAttribute(QString(),"hash"))
suppression.hash = reader.attributes().value(QString(),"hash").toULongLong();
type = reader.readNext();
if (type == QXmlStreamReader::Characters) {
suppression.errorId = reader.text().toString().toStdString();
}
mSuppressions << suppression;
}
break;
case QXmlStreamReader::EndElement:
if (reader.name().toString() != CppcheckXml::SuppressionElementName)
return;
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;
}
} while (true);
}
2020-07-17 12:25:06 +02:00
void ProjectFile::readTagWarnings(QXmlStreamReader &reader, const QString &tag)
{
QXmlStreamReader::TokenType type;
do {
type = reader.readNext();
switch (type) {
case QXmlStreamReader::StartElement:
// Read library-elements
if (reader.name().toString() == CppcheckXml::WarningElementName) {
2020-07-21 11:27:03 +02:00
std::size_t hash = reader.attributes().value(QString(), CppcheckXml::HashAttributeName).toULongLong();
mWarningTags[hash] = tag;
}
break;
case QXmlStreamReader::EndElement:
if (reader.name().toString() != CppcheckXml::WarningElementName)
return;
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;
}
} while (true);
}
2017-07-28 05:08:59 +02:00
void ProjectFile::readStringList(QStringList &stringlist, QXmlStreamReader &reader, const char elementname[])
2013-12-29 18:06:31 +01:00
{
QXmlStreamReader::TokenType type;
bool allRead = false;
do {
type = reader.readNext();
switch (type) {
case QXmlStreamReader::StartElement:
// Read library-elements
2013-12-30 22:32:50 +01:00
if (reader.name().toString() == elementname) {
2013-12-29 18:06:31 +01:00
type = reader.readNext();
if (type == QXmlStreamReader::Characters) {
2013-12-30 22:32:50 +01:00
QString text = reader.text().toString();
stringlist << text;
2013-12-29 18:06:31 +01:00
}
}
break;
case QXmlStreamReader::EndElement:
2013-12-30 22:32:50 +01:00
if (reader.name().toString() != elementname)
2013-12-29 18:06:31 +01:00
allRead = true;
break;
// Not handled
2013-12-29 18:06:31 +01:00
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;
}
} while (!allRead);
}
2017-07-28 05:08:59 +02:00
void ProjectFile::setIncludes(const QStringList &includes)
{
mIncludeDirs = includes;
}
2017-07-28 05:08:59 +02:00
void ProjectFile::setDefines(const QStringList &defines)
{
mDefines = defines;
}
void ProjectFile::setUndefines(const QStringList &undefines)
{
mUndefines = undefines;
}
2017-07-28 05:08:59 +02:00
void ProjectFile::setCheckPaths(const QStringList &paths)
{
mPaths = paths;
}
2017-07-28 05:08:59 +02:00
void ProjectFile::setExcludedPaths(const QStringList &paths)
{
mExcludedPaths = paths;
}
2017-07-28 05:08:59 +02:00
void ProjectFile::setLibraries(const QStringList &libraries)
2013-12-29 18:06:31 +01:00
{
mLibraries = libraries;
}
void ProjectFile::setFunctionContract(QString function, QString expects)
{
mFunctionContracts[function.toStdString()] = expects.toStdString();
}
void ProjectFile::setPlatform(const QString &platform)
{
mPlatform = platform;
}
void ProjectFile::setSuppressions(const QList<Suppressions::Suppression> &suppressions)
2013-12-30 22:32:50 +01:00
{
mSuppressions = suppressions;
}
void ProjectFile::addSuppression(const Suppressions::Suppression &suppression)
{
mSuppressions.append(suppression);
}
void ProjectFile::setAddons(const QStringList &addons)
{
mAddons = addons;
}
void ProjectFile::setVSConfigurations(const QStringList &vsConfigs)
{
mVsConfigurations = vsConfigs;
}
2020-07-21 11:27:03 +02:00
void ProjectFile::setWarningTags(std::size_t hash, QString tag)
{
if (tag.isEmpty())
2020-07-21 11:27:03 +02:00
mWarningTags.erase(hash);
else if (hash > 0)
mWarningTags[hash] = tag;
}
2020-07-21 11:27:03 +02:00
QString ProjectFile::getWarningTags(std::size_t hash) const
{
2020-07-21 11:27:03 +02:00
auto it = mWarningTags.find(hash);
return (it != mWarningTags.end()) ? it->second : QString();
}
2017-07-28 05:08:59 +02:00
bool ProjectFile::write(const QString &filename)
{
if (!filename.isEmpty())
mFilename = filename;
QFile file(mFilename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return false;
QXmlStreamWriter xmlWriter(&file);
xmlWriter.setAutoFormatting(true);
xmlWriter.writeStartDocument("1.0");
xmlWriter.writeStartElement(CppcheckXml::ProjectElementName);
xmlWriter.writeAttribute(CppcheckXml::ProjectVersionAttrib, CppcheckXml::ProjectFileVersion);
2011-10-13 20:53:06 +02:00
if (!mRootPath.isEmpty()) {
xmlWriter.writeStartElement(CppcheckXml::RootPathName);
xmlWriter.writeAttribute(CppcheckXml::RootPathNameAttrib, mRootPath);
xmlWriter.writeEndElement();
}
2016-11-19 20:38:50 +01:00
if (!mBuildDir.isEmpty()) {
xmlWriter.writeStartElement(CppcheckXml::BuildDirElementName);
2016-11-19 20:38:50 +01:00
xmlWriter.writeCharacters(mBuildDir);
xmlWriter.writeEndElement();
}
if (!mPlatform.isEmpty()) {
xmlWriter.writeStartElement(CppcheckXml::PlatformElementName);
xmlWriter.writeCharacters(mPlatform);
xmlWriter.writeEndElement();
}
2016-08-18 21:58:50 +02:00
if (!mImportProject.isEmpty()) {
xmlWriter.writeStartElement(CppcheckXml::ImportProjectElementName);
2016-08-18 21:58:50 +02:00
xmlWriter.writeCharacters(mImportProject);
xmlWriter.writeEndElement();
}
xmlWriter.writeStartElement(CppcheckXml::AnalyzeAllVsConfigsElementName);
xmlWriter.writeCharacters(mAnalyzeAllVsConfigs ? "true" : "false");
xmlWriter.writeEndElement();
2020-02-02 10:45:42 +01:00
if (clangParser) {
xmlWriter.writeStartElement(CppcheckXml::Parser);
xmlWriter.writeCharacters("clang");
xmlWriter.writeEndElement();
}
if (bugHunting) {
xmlWriter.writeStartElement(CppcheckXml::BugHunting);
xmlWriter.writeEndElement();
}
xmlWriter.writeStartElement(CppcheckXml::CheckHeadersElementName);
xmlWriter.writeCharacters(mCheckHeaders ? "true" : "false");
xmlWriter.writeEndElement();
xmlWriter.writeStartElement(CppcheckXml::CheckUnusedTemplatesElementName);
xmlWriter.writeCharacters(mCheckUnusedTemplates ? "true" : "false");
xmlWriter.writeEndElement();
xmlWriter.writeStartElement(CppcheckXml::MaxCtuDepthElementName);
xmlWriter.writeCharacters(QString::number(mMaxCtuDepth));
xmlWriter.writeEndElement();
xmlWriter.writeStartElement(CppcheckXml::MaxTemplateRecursionElementName);
xmlWriter.writeCharacters(QString::number(mMaxTemplateRecursion));
xmlWriter.writeEndElement();
2011-10-13 20:53:06 +02:00
if (!mIncludeDirs.isEmpty()) {
xmlWriter.writeStartElement(CppcheckXml::IncludeDirElementName);
foreach (QString incdir, mIncludeDirs) {
xmlWriter.writeStartElement(CppcheckXml::DirElementName);
xmlWriter.writeAttribute(CppcheckXml::DirNameAttrib, incdir);
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
}
2011-10-13 20:53:06 +02:00
if (!mDefines.isEmpty()) {
xmlWriter.writeStartElement(CppcheckXml::DefinesElementName);
foreach (QString define, mDefines) {
xmlWriter.writeStartElement(CppcheckXml::DefineName);
xmlWriter.writeAttribute(CppcheckXml::DefineNameAttrib, define);
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
}
if (!mVsConfigurations.isEmpty()) {
writeStringList(xmlWriter,
mVsConfigurations,
CppcheckXml::VSConfigurationElementName,
CppcheckXml::VSConfigurationName);
}
writeStringList(xmlWriter,
mUndefines,
CppcheckXml::UndefinesElementName,
CppcheckXml::UndefineName);
2011-10-13 20:53:06 +02:00
if (!mPaths.isEmpty()) {
xmlWriter.writeStartElement(CppcheckXml::PathsElementName);
foreach (QString path, mPaths) {
xmlWriter.writeStartElement(CppcheckXml::PathName);
xmlWriter.writeAttribute(CppcheckXml::PathNameAttrib, path);
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
}
2011-10-13 20:53:06 +02:00
if (!mExcludedPaths.isEmpty()) {
xmlWriter.writeStartElement(CppcheckXml::ExcludeElementName);
foreach (QString path, mExcludedPaths) {
xmlWriter.writeStartElement(CppcheckXml::ExcludePathName);
xmlWriter.writeAttribute(CppcheckXml::ExcludePathNameAttrib, path);
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
}
2017-07-28 05:08:59 +02:00
writeStringList(xmlWriter,
2013-12-30 22:32:50 +01:00
mLibraries,
CppcheckXml::LibrariesElementName,
CppcheckXml::LibraryElementName);
2013-12-30 22:32:50 +01:00
if (!mFunctionContracts.empty()) {
xmlWriter.writeStartElement(CppcheckXml::FunctionContracts);
for (const auto& contract: mFunctionContracts) {
xmlWriter.writeStartElement(CppcheckXml::FunctionContract);
xmlWriter.writeAttribute(CppcheckXml::ContractFunction, QString::fromStdString(contract.first));
xmlWriter.writeAttribute(CppcheckXml::ContractExpects, QString::fromStdString(contract.second));
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
}
2020-08-22 11:37:44 +02:00
if (!mVariableContracts.empty()) {
xmlWriter.writeStartElement(CppcheckXml::VariableContractsElementName);
for (auto vc: mVariableContracts) {
xmlWriter.writeStartElement(CppcheckXml::VariableContractItemElementName);
xmlWriter.writeAttribute(CppcheckXml::VariableContractVarName, vc.first);
xmlWriter.writeAttribute(CppcheckXml::VariableContractMin, QString::fromStdString(vc.second.minValue));
xmlWriter.writeAttribute(CppcheckXml::VariableContractMax, QString::fromStdString(vc.second.maxValue));
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
}
if (!mSuppressions.isEmpty()) {
xmlWriter.writeStartElement(CppcheckXml::SuppressionsElementName);
foreach (const Suppressions::Suppression &suppression, mSuppressions) {
xmlWriter.writeStartElement(CppcheckXml::SuppressionElementName);
if (!suppression.fileName.empty())
xmlWriter.writeAttribute("fileName", QString::fromStdString(suppression.fileName));
if (suppression.lineNumber > 0)
xmlWriter.writeAttribute("lineNumber", QString::number(suppression.lineNumber));
if (!suppression.symbolName.empty())
xmlWriter.writeAttribute("symbolName", QString::fromStdString(suppression.symbolName));
2020-07-21 11:27:03 +02:00
if (suppression.hash > 0)
xmlWriter.writeAttribute(CppcheckXml::HashAttributeName, QString::number(suppression.hash));
if (!suppression.errorId.empty())
xmlWriter.writeCharacters(QString::fromStdString(suppression.errorId));
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
}
2013-12-29 18:06:31 +01:00
writeStringList(xmlWriter,
mCheckUnknownFunctionReturn,
CppcheckXml::CheckUnknownFunctionReturn,
CppcheckXml::Name);
safeChecks.saveToXml(xmlWriter);
writeStringList(xmlWriter,
mAddons,
CppcheckXml::AddonsElementName,
CppcheckXml::AddonElementName);
QStringList tools;
if (mClangAnalyzer)
tools << CLANG_ANALYZER;
if (mClangTidy)
tools << CLANG_TIDY;
writeStringList(xmlWriter,
tools,
CppcheckXml::ToolsElementName,
CppcheckXml::ToolElementName);
writeStringList(xmlWriter, mTags, CppcheckXml::TagsElementName, CppcheckXml::TagElementName);
if (!mWarningTags.empty()) {
QStringList tags;
for (const auto& wt: mWarningTags) {
if (!tags.contains(wt.second))
tags.append(wt.second);
}
for (const QString &tag: tags) {
xmlWriter.writeStartElement(CppcheckXml::TagWarningsElementName);
xmlWriter.writeAttribute(CppcheckXml::TagAttributeName, tag);
QStringList warnings;
for (const auto& wt: mWarningTags) {
if (wt.second == tag) {
xmlWriter.writeStartElement(CppcheckXml::WarningElementName);
2020-07-21 11:27:03 +02:00
xmlWriter.writeAttribute(CppcheckXml::HashAttributeName, QString::number(wt.first));
xmlWriter.writeEndElement();
}
}
xmlWriter.writeEndElement();
}
}
xmlWriter.writeEndDocument();
file.close();
return true;
}
2013-12-30 22:32:50 +01:00
2017-07-28 05:08:59 +02:00
void ProjectFile::writeStringList(QXmlStreamWriter &xmlWriter, const QStringList &stringlist, const char startelementname[], const char stringelementname[])
2013-12-30 22:32:50 +01:00
{
if (stringlist.isEmpty())
return;
xmlWriter.writeStartElement(startelementname);
foreach (QString str, stringlist) {
2013-12-30 22:32:50 +01:00
xmlWriter.writeStartElement(stringelementname);
xmlWriter.writeCharacters(str);
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
}
2016-10-02 18:03:21 +02:00
QStringList ProjectFile::fromNativeSeparators(const QStringList &paths)
{
QStringList ret;
foreach (const QString &path, paths)
ret << QDir::fromNativeSeparators(path);
return ret;
}
2017-09-22 18:57:53 +02:00
QStringList ProjectFile::getAddonsAndTools() const
{
QStringList ret(mAddons);
if (mClangAnalyzer)
ret << CLANG_ANALYZER;
if (mClangTidy)
ret << CLANG_TIDY;
return ret;
}
void ProjectFile::SafeChecks::loadFromXml(QXmlStreamReader &xmlReader)
{
classes = externalFunctions = internalFunctions = externalVariables = false;
int level = 0;
do {
const QXmlStreamReader::TokenType type = xmlReader.readNext();
switch (type) {
case QXmlStreamReader::StartElement:
++level;
if (xmlReader.name() == Settings::SafeChecks::XmlClasses)
classes = true;
else if (xmlReader.name() == Settings::SafeChecks::XmlExternalFunctions)
externalFunctions = true;
else if (xmlReader.name() == Settings::SafeChecks::XmlInternalFunctions)
internalFunctions = true;
else if (xmlReader.name() == Settings::SafeChecks::XmlExternalVariables)
externalVariables = true;
break;
case QXmlStreamReader::EndElement:
if (level <= 0)
return;
level--;
break;
// Not handled
case QXmlStreamReader::Characters:
case QXmlStreamReader::NoToken:
case QXmlStreamReader::Invalid:
case QXmlStreamReader::StartDocument:
case QXmlStreamReader::EndDocument:
case QXmlStreamReader::Comment:
case QXmlStreamReader::DTD:
case QXmlStreamReader::EntityReference:
case QXmlStreamReader::ProcessingInstruction:
break;
}
} while (true);
}
void ProjectFile::SafeChecks::saveToXml(QXmlStreamWriter &xmlWriter) const
{
if (!classes && !externalFunctions && !internalFunctions && !externalVariables)
return;
xmlWriter.writeStartElement(Settings::SafeChecks::XmlRootName);
if (classes) {
xmlWriter.writeStartElement(Settings::SafeChecks::XmlClasses);
xmlWriter.writeEndElement();
}
if (externalFunctions) {
xmlWriter.writeStartElement(Settings::SafeChecks::XmlExternalFunctions);
xmlWriter.writeEndElement();
}
if (internalFunctions) {
xmlWriter.writeStartElement(Settings::SafeChecks::XmlInternalFunctions);
xmlWriter.writeEndElement();
}
if (externalVariables) {
xmlWriter.writeStartElement(Settings::SafeChecks::XmlExternalVariables);
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
}
2020-05-17 23:14:40 +02:00
QString ProjectFile::getAddonFilePath(QString filesDir, const QString &addon)
{
if (!filesDir.endsWith("/"))
filesDir += "/";
QStringList searchPaths;
searchPaths << filesDir << (filesDir + "addons/") << (filesDir + "../addons/")
#ifdef FILESDIR
<< (QLatin1String(FILESDIR) + "/addons/")
#endif
;
foreach (QString path, searchPaths) {
QString f = path + addon + ".py";
if (QFile(f).exists())
return f;
}
return QString();
}