Refactor load/write of cppcheck project. Reuse same string constants in cli and gui.

This commit is contained in:
Daniel Marjamäki 2019-08-09 21:15:02 +02:00
parent 16dc8a0540
commit 0fbbe2ff23
4 changed files with 165 additions and 204 deletions

View File

@ -545,10 +545,8 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
ImportProject::Type projType = mSettings->project.import(projectFile, mSettings); ImportProject::Type projType = mSettings->project.import(projectFile, mSettings);
if (projType == ImportProject::Type::CPPCHECK_GUI) { if (projType == ImportProject::Type::CPPCHECK_GUI) {
mPathNames = mSettings->project.guiProject.pathNames; mPathNames = mSettings->project.guiProject.pathNames;
for (const std::string &lib : mSettings->project.guiProject.libraries) { for (const std::string &lib : mSettings->project.guiProject.libraries)
if (!CppCheckExecutor::tryLoadLibrary(mSettings->library, argv[0], lib.c_str())) mSettings->libraries.push_back(lib);
return false;
}
for (const std::string &ignorePath : mSettings->project.guiProject.excludedPaths) for (const std::string &ignorePath : mSettings->project.guiProject.excludedPaths)
mIgnoredPaths.emplace_back(ignorePath); mIgnoredPaths.emplace_back(ignorePath);

View File

@ -24,52 +24,11 @@
#include <QDir> #include <QDir>
#include "projectfile.h" #include "projectfile.h"
#include "common.h" #include "common.h"
#include "importproject.h"
#include "path.h" #include "path.h"
#include "settings.h" #include "settings.h"
static const char ProjectElementName[] = "project";
static const char ProjectVersionAttrib[] = "version";
static const char ProjectFileVersion[] = "1";
static const char BuildDirElementName[] = "builddir";
static const char ImportProjectElementName[] = "importproject";
static const char AnalyzeAllVsConfigsElementName[] = "analyze-all-vs-configs";
static const char IncludeDirElementName[] = "includedir";
static const char DirElementName[] = "dir";
static const char DirNameAttrib[] = "name";
static const char DefinesElementName[] = "defines";
static const char DefineName[] = "define";
static const char DefineNameAttrib[] = "name";
static const char UndefinesElementName[] = "undefines";
static const char UndefineName[] = "undefine";
static const char PathsElementName[] = "paths";
static const char PathName[] = "dir";
static const char PathNameAttrib[] = "name";
static const char RootPathName[] = "root";
static const char RootPathNameAttrib[] = "name";
static const char IgnoreElementName[] = "ignore";
static const char IgnorePathName[] = "path";
static const char IgnorePathNameAttrib[] = "name";
static const char ExcludeElementName[] = "exclude";
static const char ExcludePathName[] = "path";
static const char ExcludePathNameAttrib[] = "name";
static const char LibrariesElementName[] = "libraries";
static const char LibraryElementName[] = "library";
static const char PlatformElementName[] = "platform";
static const char SuppressionsElementName[] = "suppressions";
static const char SuppressionElementName[] = "suppression";
static const char AddonElementName[] = "addon";
static const char AddonsElementName[] = "addons";
static const char ToolElementName[] = "tool";
static const char ToolsElementName[] = "tools";
static const char TagsElementName[] = "tags";
static const char TagElementName[] = "tag";
static const char CheckHeadersElementName[] = "check-headers";
static const char CheckUnusedTemplatesElementName[] = "check-unused-templates";
static const char MaxCtuDepthElementName[] = "max-ctu-depth";
static const char CheckUnknownFunctionReturn[] = "check-unknown-function-return-values";
static const char Name[] = "name";
ProjectFile::ProjectFile(QObject *parent) : ProjectFile::ProjectFile(QObject *parent) :
QObject(parent) QObject(parent)
{ {
@ -125,7 +84,7 @@ bool ProjectFile::read(const QString &filename)
while (!xmlReader.atEnd()) { while (!xmlReader.atEnd()) {
switch (xmlReader.readNext()) { switch (xmlReader.readNext()) {
case QXmlStreamReader::StartElement: case QXmlStreamReader::StartElement:
if (xmlReader.name() == ProjectElementName) { if (xmlReader.name() == CppcheckXml::ProjectElementName) {
insideProject = true; insideProject = true;
projectTagFound = true; projectTagFound = true;
break; break;
@ -134,91 +93,91 @@ bool ProjectFile::read(const QString &filename)
break; break;
// Read root path from inside project element // Read root path from inside project element
if (xmlReader.name() == RootPathName) if (xmlReader.name() == CppcheckXml::RootPathName)
readRootPath(xmlReader); readRootPath(xmlReader);
// Read root path from inside project element // Read root path from inside project element
if (xmlReader.name() == BuildDirElementName) if (xmlReader.name() == CppcheckXml::BuildDirElementName)
readBuildDir(xmlReader); readBuildDir(xmlReader);
// Find paths to check from inside project element // Find paths to check from inside project element
if (xmlReader.name() == PathsElementName) if (xmlReader.name() == CppcheckXml::PathsElementName)
readCheckPaths(xmlReader); readCheckPaths(xmlReader);
if (xmlReader.name() == ImportProjectElementName) if (xmlReader.name() == CppcheckXml::ImportProjectElementName)
readImportProject(xmlReader); readImportProject(xmlReader);
if (xmlReader.name() == AnalyzeAllVsConfigsElementName) if (xmlReader.name() == CppcheckXml::AnalyzeAllVsConfigsElementName)
mAnalyzeAllVsConfigs = readBool(xmlReader); mAnalyzeAllVsConfigs = readBool(xmlReader);
if (xmlReader.name() == CheckHeadersElementName) if (xmlReader.name() == CppcheckXml::CheckHeadersElementName)
mCheckHeaders = readBool(xmlReader); mCheckHeaders = readBool(xmlReader);
if (xmlReader.name() == CheckUnusedTemplatesElementName) if (xmlReader.name() == CppcheckXml::CheckUnusedTemplatesElementName)
mCheckUnusedTemplates = readBool(xmlReader); mCheckUnusedTemplates = readBool(xmlReader);
// Find include directory from inside project element // Find include directory from inside project element
if (xmlReader.name() == IncludeDirElementName) if (xmlReader.name() == CppcheckXml::IncludeDirElementName)
readIncludeDirs(xmlReader); readIncludeDirs(xmlReader);
// Find preprocessor define from inside project element // Find preprocessor define from inside project element
if (xmlReader.name() == DefinesElementName) if (xmlReader.name() == CppcheckXml::DefinesElementName)
readDefines(xmlReader); readDefines(xmlReader);
// Find preprocessor define from inside project element // Find preprocessor define from inside project element
if (xmlReader.name() == UndefinesElementName) if (xmlReader.name() == CppcheckXml::UndefinesElementName)
readStringList(mUndefines, xmlReader, UndefineName); readStringList(mUndefines, xmlReader, CppcheckXml::UndefineName);
// Find exclude list from inside project element // Find exclude list from inside project element
if (xmlReader.name() == ExcludeElementName) if (xmlReader.name() == CppcheckXml::ExcludeElementName)
readExcludes(xmlReader); readExcludes(xmlReader);
// Find ignore list from inside project element // Find ignore list from inside project element
// These are read for compatibility // These are read for compatibility
if (xmlReader.name() == IgnoreElementName) if (xmlReader.name() == CppcheckXml::IgnoreElementName)
readExcludes(xmlReader); readExcludes(xmlReader);
// Find libraries list from inside project element // Find libraries list from inside project element
if (xmlReader.name() == LibrariesElementName) if (xmlReader.name() == CppcheckXml::LibrariesElementName)
readStringList(mLibraries, xmlReader,LibraryElementName); readStringList(mLibraries, xmlReader, CppcheckXml::LibraryElementName);
if (xmlReader.name() == PlatformElementName) if (xmlReader.name() == CppcheckXml::PlatformElementName)
readPlatform(xmlReader); readPlatform(xmlReader);
// Find suppressions list from inside project element // Find suppressions list from inside project element
if (xmlReader.name() == SuppressionsElementName) if (xmlReader.name() == CppcheckXml::SuppressionsElementName)
readSuppressions(xmlReader); readSuppressions(xmlReader);
// Unknown function return values // Unknown function return values
if (xmlReader.name() == CheckUnknownFunctionReturn) if (xmlReader.name() == CppcheckXml::CheckUnknownFunctionReturn)
readStringList(mCheckUnknownFunctionReturn, xmlReader, Name); readStringList(mCheckUnknownFunctionReturn, xmlReader, CppcheckXml::Name);
// check all function parameter values // check all function parameter values
if (xmlReader.name() == Settings::SafeChecks::XmlRootName) if (xmlReader.name() == Settings::SafeChecks::XmlRootName)
mSafeChecks.loadFromXml(xmlReader); mSafeChecks.loadFromXml(xmlReader);
// Addons // Addons
if (xmlReader.name() == AddonsElementName) if (xmlReader.name() == CppcheckXml::AddonsElementName)
readStringList(mAddons, xmlReader, AddonElementName); readStringList(mAddons, xmlReader, CppcheckXml::AddonElementName);
// Tools // Tools
if (xmlReader.name() == ToolsElementName) { if (xmlReader.name() == CppcheckXml::ToolsElementName) {
QStringList tools; QStringList tools;
readStringList(tools, xmlReader, ToolElementName); readStringList(tools, xmlReader, CppcheckXml::ToolElementName);
mClangAnalyzer = tools.contains(CLANG_ANALYZER); mClangAnalyzer = tools.contains(CLANG_ANALYZER);
mClangTidy = tools.contains(CLANG_TIDY); mClangTidy = tools.contains(CLANG_TIDY);
} }
if (insideProject && xmlReader.name() == TagsElementName) if (insideProject && xmlReader.name() == CppcheckXml::TagsElementName)
readStringList(mTags, xmlReader, TagElementName); readStringList(mTags, xmlReader, CppcheckXml::TagElementName);
if (insideProject && xmlReader.name() == MaxCtuDepthElementName) if (insideProject && xmlReader.name() == CppcheckXml::MaxCtuDepthElementName)
mMaxCtuDepth = readInt(xmlReader, mMaxCtuDepth); mMaxCtuDepth = readInt(xmlReader, mMaxCtuDepth);
break; break;
case QXmlStreamReader::EndElement: case QXmlStreamReader::EndElement:
if (xmlReader.name() == ProjectElementName) if (xmlReader.name() == CppcheckXml::ProjectElementName)
insideProject = false; insideProject = false;
break; break;
@ -243,7 +202,7 @@ bool ProjectFile::read(const QString &filename)
void ProjectFile::readRootPath(QXmlStreamReader &reader) void ProjectFile::readRootPath(QXmlStreamReader &reader)
{ {
QXmlStreamAttributes attribs = reader.attributes(); QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value(QString(), RootPathNameAttrib).toString(); QString name = attribs.value(QString(), CppcheckXml::RootPathNameAttrib).toString();
if (!name.isEmpty()) if (!name.isEmpty())
mRootPath = name; mRootPath = name;
} }
@ -358,16 +317,16 @@ void ProjectFile::readIncludeDirs(QXmlStreamReader &reader)
case QXmlStreamReader::StartElement: case QXmlStreamReader::StartElement:
// Read dir-elements // Read dir-elements
if (reader.name().toString() == DirElementName) { if (reader.name().toString() == CppcheckXml::DirElementName) {
QXmlStreamAttributes attribs = reader.attributes(); QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value(QString(), DirNameAttrib).toString(); QString name = attribs.value(QString(), CppcheckXml::DirNameAttrib).toString();
if (!name.isEmpty()) if (!name.isEmpty())
mIncludeDirs << name; mIncludeDirs << name;
} }
break; break;
case QXmlStreamReader::EndElement: case QXmlStreamReader::EndElement:
if (reader.name().toString() == IncludeDirElementName) if (reader.name().toString() == CppcheckXml::IncludeDirElementName)
allRead = true; allRead = true;
break; break;
@ -395,16 +354,16 @@ void ProjectFile::readDefines(QXmlStreamReader &reader)
switch (type) { switch (type) {
case QXmlStreamReader::StartElement: case QXmlStreamReader::StartElement:
// Read define-elements // Read define-elements
if (reader.name().toString() == DefineName) { if (reader.name().toString() == CppcheckXml::DefineName) {
QXmlStreamAttributes attribs = reader.attributes(); QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value(QString(), DefineNameAttrib).toString(); QString name = attribs.value(QString(), CppcheckXml::DefineNameAttrib).toString();
if (!name.isEmpty()) if (!name.isEmpty())
mDefines << name; mDefines << name;
} }
break; break;
case QXmlStreamReader::EndElement: case QXmlStreamReader::EndElement:
if (reader.name().toString() == DefinesElementName) if (reader.name().toString() == CppcheckXml::DefinesElementName)
allRead = true; allRead = true;
break; break;
@ -433,16 +392,16 @@ void ProjectFile::readCheckPaths(QXmlStreamReader &reader)
case QXmlStreamReader::StartElement: case QXmlStreamReader::StartElement:
// Read dir-elements // Read dir-elements
if (reader.name().toString() == PathName) { if (reader.name().toString() == CppcheckXml::PathName) {
QXmlStreamAttributes attribs = reader.attributes(); QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value(QString(), PathNameAttrib).toString(); QString name = attribs.value(QString(), CppcheckXml::PathNameAttrib).toString();
if (!name.isEmpty()) if (!name.isEmpty())
mPaths << name; mPaths << name;
} }
break; break;
case QXmlStreamReader::EndElement: case QXmlStreamReader::EndElement:
if (reader.name().toString() == PathsElementName) if (reader.name().toString() == CppcheckXml::PathsElementName)
allRead = true; allRead = true;
break; break;
@ -470,25 +429,25 @@ void ProjectFile::readExcludes(QXmlStreamReader &reader)
switch (type) { switch (type) {
case QXmlStreamReader::StartElement: case QXmlStreamReader::StartElement:
// Read exclude-elements // Read exclude-elements
if (reader.name().toString() == ExcludePathName) { if (reader.name().toString() == CppcheckXml::ExcludePathName) {
QXmlStreamAttributes attribs = reader.attributes(); QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value(QString(), ExcludePathNameAttrib).toString(); QString name = attribs.value(QString(), CppcheckXml::ExcludePathNameAttrib).toString();
if (!name.isEmpty()) if (!name.isEmpty())
mExcludedPaths << name; mExcludedPaths << name;
} }
// Read ignore-elements - deprecated but support reading them // Read ignore-elements - deprecated but support reading them
else if (reader.name().toString() == IgnorePathName) { else if (reader.name().toString() == CppcheckXml::IgnorePathName) {
QXmlStreamAttributes attribs = reader.attributes(); QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value(QString(), IgnorePathNameAttrib).toString(); QString name = attribs.value(QString(), CppcheckXml::IgnorePathNameAttrib).toString();
if (!name.isEmpty()) if (!name.isEmpty())
mExcludedPaths << name; mExcludedPaths << name;
} }
break; break;
case QXmlStreamReader::EndElement: case QXmlStreamReader::EndElement:
if (reader.name().toString() == IgnoreElementName) if (reader.name().toString() == CppcheckXml::IgnoreElementName)
allRead = true; allRead = true;
if (reader.name().toString() == ExcludeElementName) if (reader.name().toString() == CppcheckXml::ExcludeElementName)
allRead = true; allRead = true;
break; break;
@ -540,7 +499,7 @@ void ProjectFile::readSuppressions(QXmlStreamReader &reader)
switch (type) { switch (type) {
case QXmlStreamReader::StartElement: case QXmlStreamReader::StartElement:
// Read library-elements // Read library-elements
if (reader.name().toString() == SuppressionElementName) { if (reader.name().toString() == CppcheckXml::SuppressionElementName) {
Suppressions::Suppression suppression; Suppressions::Suppression suppression;
if (reader.attributes().hasAttribute(QString(),"fileName")) if (reader.attributes().hasAttribute(QString(),"fileName"))
suppression.fileName = reader.attributes().value(QString(),"fileName").toString().toStdString(); suppression.fileName = reader.attributes().value(QString(),"fileName").toString().toStdString();
@ -557,7 +516,7 @@ void ProjectFile::readSuppressions(QXmlStreamReader &reader)
break; break;
case QXmlStreamReader::EndElement: case QXmlStreamReader::EndElement:
if (reader.name().toString() != SuppressionElementName) if (reader.name().toString() != CppcheckXml::SuppressionElementName)
return; return;
break; break;
@ -685,64 +644,64 @@ bool ProjectFile::write(const QString &filename)
QXmlStreamWriter xmlWriter(&file); QXmlStreamWriter xmlWriter(&file);
xmlWriter.setAutoFormatting(true); xmlWriter.setAutoFormatting(true);
xmlWriter.writeStartDocument("1.0"); xmlWriter.writeStartDocument("1.0");
xmlWriter.writeStartElement(ProjectElementName); xmlWriter.writeStartElement(CppcheckXml::ProjectElementName);
xmlWriter.writeAttribute(ProjectVersionAttrib, ProjectFileVersion); xmlWriter.writeAttribute(CppcheckXml::ProjectVersionAttrib, CppcheckXml::ProjectFileVersion);
if (!mRootPath.isEmpty()) { if (!mRootPath.isEmpty()) {
xmlWriter.writeStartElement(RootPathName); xmlWriter.writeStartElement(CppcheckXml::RootPathName);
xmlWriter.writeAttribute(RootPathNameAttrib, mRootPath); xmlWriter.writeAttribute(CppcheckXml::RootPathNameAttrib, mRootPath);
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
} }
if (!mBuildDir.isEmpty()) { if (!mBuildDir.isEmpty()) {
xmlWriter.writeStartElement(BuildDirElementName); xmlWriter.writeStartElement(CppcheckXml::BuildDirElementName);
xmlWriter.writeCharacters(mBuildDir); xmlWriter.writeCharacters(mBuildDir);
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
} }
if (!mPlatform.isEmpty()) { if (!mPlatform.isEmpty()) {
xmlWriter.writeStartElement(PlatformElementName); xmlWriter.writeStartElement(CppcheckXml::PlatformElementName);
xmlWriter.writeCharacters(mPlatform); xmlWriter.writeCharacters(mPlatform);
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
} }
if (!mImportProject.isEmpty()) { if (!mImportProject.isEmpty()) {
xmlWriter.writeStartElement(ImportProjectElementName); xmlWriter.writeStartElement(CppcheckXml::ImportProjectElementName);
xmlWriter.writeCharacters(mImportProject); xmlWriter.writeCharacters(mImportProject);
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
} }
xmlWriter.writeStartElement(AnalyzeAllVsConfigsElementName); xmlWriter.writeStartElement(CppcheckXml::AnalyzeAllVsConfigsElementName);
xmlWriter.writeCharacters(mAnalyzeAllVsConfigs ? "true" : "false"); xmlWriter.writeCharacters(mAnalyzeAllVsConfigs ? "true" : "false");
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
xmlWriter.writeStartElement(CheckHeadersElementName); xmlWriter.writeStartElement(CppcheckXml::CheckHeadersElementName);
xmlWriter.writeCharacters(mCheckHeaders ? "true" : "false"); xmlWriter.writeCharacters(mCheckHeaders ? "true" : "false");
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
xmlWriter.writeStartElement(CheckUnusedTemplatesElementName); xmlWriter.writeStartElement(CppcheckXml::CheckUnusedTemplatesElementName);
xmlWriter.writeCharacters(mCheckUnusedTemplates ? "true" : "false"); xmlWriter.writeCharacters(mCheckUnusedTemplates ? "true" : "false");
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
xmlWriter.writeStartElement(MaxCtuDepthElementName); xmlWriter.writeStartElement(CppcheckXml::MaxCtuDepthElementName);
xmlWriter.writeCharacters(QString::number(mMaxCtuDepth)); xmlWriter.writeCharacters(QString::number(mMaxCtuDepth));
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
if (!mIncludeDirs.isEmpty()) { if (!mIncludeDirs.isEmpty()) {
xmlWriter.writeStartElement(IncludeDirElementName); xmlWriter.writeStartElement(CppcheckXml::IncludeDirElementName);
foreach (QString incdir, mIncludeDirs) { foreach (QString incdir, mIncludeDirs) {
xmlWriter.writeStartElement(DirElementName); xmlWriter.writeStartElement(CppcheckXml::DirElementName);
xmlWriter.writeAttribute(DirNameAttrib, incdir); xmlWriter.writeAttribute(CppcheckXml::DirNameAttrib, incdir);
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
} }
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
} }
if (!mDefines.isEmpty()) { if (!mDefines.isEmpty()) {
xmlWriter.writeStartElement(DefinesElementName); xmlWriter.writeStartElement(CppcheckXml::DefinesElementName);
foreach (QString define, mDefines) { foreach (QString define, mDefines) {
xmlWriter.writeStartElement(DefineName); xmlWriter.writeStartElement(CppcheckXml::DefineName);
xmlWriter.writeAttribute(DefineNameAttrib, define); xmlWriter.writeAttribute(CppcheckXml::DefineNameAttrib, define);
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
} }
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
@ -750,24 +709,24 @@ bool ProjectFile::write(const QString &filename)
writeStringList(xmlWriter, writeStringList(xmlWriter,
mUndefines, mUndefines,
UndefinesElementName, CppcheckXml::UndefinesElementName,
UndefineName); CppcheckXml::UndefineName);
if (!mPaths.isEmpty()) { if (!mPaths.isEmpty()) {
xmlWriter.writeStartElement(PathsElementName); xmlWriter.writeStartElement(CppcheckXml::PathsElementName);
foreach (QString path, mPaths) { foreach (QString path, mPaths) {
xmlWriter.writeStartElement(PathName); xmlWriter.writeStartElement(CppcheckXml::PathName);
xmlWriter.writeAttribute(PathNameAttrib, path); xmlWriter.writeAttribute(CppcheckXml::PathNameAttrib, path);
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
} }
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
} }
if (!mExcludedPaths.isEmpty()) { if (!mExcludedPaths.isEmpty()) {
xmlWriter.writeStartElement(ExcludeElementName); xmlWriter.writeStartElement(CppcheckXml::ExcludeElementName);
foreach (QString path, mExcludedPaths) { foreach (QString path, mExcludedPaths) {
xmlWriter.writeStartElement(ExcludePathName); xmlWriter.writeStartElement(CppcheckXml::ExcludePathName);
xmlWriter.writeAttribute(ExcludePathNameAttrib, path); xmlWriter.writeAttribute(CppcheckXml::ExcludePathNameAttrib, path);
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
} }
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
@ -775,13 +734,13 @@ bool ProjectFile::write(const QString &filename)
writeStringList(xmlWriter, writeStringList(xmlWriter,
mLibraries, mLibraries,
LibrariesElementName, CppcheckXml::LibrariesElementName,
LibraryElementName); CppcheckXml::LibraryElementName);
if (!mSuppressions.isEmpty()) { if (!mSuppressions.isEmpty()) {
xmlWriter.writeStartElement(SuppressionsElementName); xmlWriter.writeStartElement(CppcheckXml::SuppressionsElementName);
foreach (const Suppressions::Suppression &suppression, mSuppressions) { foreach (const Suppressions::Suppression &suppression, mSuppressions) {
xmlWriter.writeStartElement(SuppressionElementName); xmlWriter.writeStartElement(CppcheckXml::SuppressionElementName);
if (!suppression.fileName.empty()) if (!suppression.fileName.empty())
xmlWriter.writeAttribute("fileName", QString::fromStdString(suppression.fileName)); xmlWriter.writeAttribute("fileName", QString::fromStdString(suppression.fileName));
if (suppression.lineNumber > 0) if (suppression.lineNumber > 0)
@ -797,15 +756,15 @@ bool ProjectFile::write(const QString &filename)
writeStringList(xmlWriter, writeStringList(xmlWriter,
mCheckUnknownFunctionReturn, mCheckUnknownFunctionReturn,
CheckUnknownFunctionReturn, CppcheckXml::CheckUnknownFunctionReturn,
Name); CppcheckXml::Name);
mSafeChecks.saveToXml(xmlWriter); mSafeChecks.saveToXml(xmlWriter);
writeStringList(xmlWriter, writeStringList(xmlWriter,
mAddons, mAddons,
AddonsElementName, CppcheckXml::AddonsElementName,
AddonElementName); CppcheckXml::AddonElementName);
QStringList tools; QStringList tools;
if (mClangAnalyzer) if (mClangAnalyzer)
@ -814,10 +773,10 @@ bool ProjectFile::write(const QString &filename)
tools << CLANG_TIDY; tools << CLANG_TIDY;
writeStringList(xmlWriter, writeStringList(xmlWriter,
tools, tools,
ToolsElementName, CppcheckXml::ToolsElementName,
ToolElementName); CppcheckXml::ToolElementName);
writeStringList(xmlWriter, mTags, TagsElementName, TagElementName); writeStringList(xmlWriter, mTags, CppcheckXml::TagsElementName, CppcheckXml::TagElementName);
xmlWriter.writeEndDocument(); xmlWriter.writeEndDocument();
file.close(); file.close();

View File

@ -33,6 +33,7 @@
#include <utility> #include <utility>
#include <sstream> #include <sstream>
void ImportProject::ignorePaths(const std::vector<std::string> &ipaths) void ImportProject::ignorePaths(const std::vector<std::string> &ipaths)
{ {
for (std::list<FileSettings>::iterator it = fileSettings.begin(); it != fileSettings.end();) { for (std::list<FileSettings>::iterator it = fileSettings.begin(); it != fileSettings.end();) {
@ -959,47 +960,6 @@ static std::string join(const std::list<std::string> &strlist, const char *sep)
return ret; return ret;
} }
// These constants are copy/pasted from gui/projectfile.cpp
static const char ProjectElementName[] = "project";
static const char ProjectVersionAttrib[] = "version";
static const char ProjectFileVersion[] = "1";
static const char BuildDirElementName[] = "builddir";
static const char ImportProjectElementName[] = "importproject";
static const char AnalyzeAllVsConfigsElementName[] = "analyze-all-vs-configs";
static const char IncludeDirElementName[] = "includedir";
static const char DirElementName[] = "dir";
static const char DirNameAttrib[] = "name";
static const char DefinesElementName[] = "defines";
static const char DefineName[] = "define";
static const char DefineNameAttrib[] = "name";
static const char UndefinesElementName[] = "undefines";
static const char UndefineName[] = "undefine";
static const char PathsElementName[] = "paths";
static const char PathName[] = "dir";
static const char PathNameAttrib[] = "name";
static const char RootPathName[] = "root";
static const char RootPathNameAttrib[] = "name";
static const char IgnoreElementName[] = "ignore";
static const char IgnorePathName[] = "path";
static const char IgnorePathNameAttrib[] = "name";
static const char ExcludeElementName[] = "exclude";
static const char ExcludePathName[] = "path";
static const char ExcludePathNameAttrib[] = "name";
static const char LibrariesElementName[] = "libraries";
static const char LibraryElementName[] = "library";
static const char PlatformElementName[] = "platform";
static const char SuppressionsElementName[] = "suppressions";
static const char SuppressionElementName[] = "suppression";
static const char AddonElementName[] = "addon";
static const char AddonsElementName[] = "addons";
static const char ToolElementName[] = "tool";
static const char ToolsElementName[] = "tools";
static const char TagsElementName[] = "tags";
static const char TagElementName[] = "tag";
static const char CheckHeadersElementName[] = "check-headers";
static const char CheckUnusedTemplatesElementName[] = "check-unused-templates";
static const char MaxCtuDepthElementName[] = "max-ctu-depth";
static std::string istream_to_string(std::istream &istr) static std::string istream_to_string(std::istream &istr)
{ {
std::istreambuf_iterator<char> eos; std::istreambuf_iterator<char> eos;
@ -1013,12 +973,9 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *setti
if (doc.Parse(xmldata.data(), xmldata.size()) != tinyxml2::XML_SUCCESS) if (doc.Parse(xmldata.data(), xmldata.size()) != tinyxml2::XML_SUCCESS)
return false; return false;
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement(); const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
if (rootnode == nullptr || strcmp(rootnode->Name(), ProjectElementName) != 0) if (rootnode == nullptr || strcmp(rootnode->Name(), CppcheckXml::ProjectElementName) != 0)
return false; return false;
(void)ProjectFileVersion;
(void)ProjectVersionAttrib;
const std::string &path = mPath; const std::string &path = mPath;
std::list<std::string> paths; std::list<std::string> paths;
@ -1028,46 +985,48 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *setti
guiProject.analyzeAllVsConfigs.clear(); guiProject.analyzeAllVsConfigs.clear();
for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) { for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
if (strcmp(node->Name(), RootPathName) == 0 && node->Attribute(RootPathNameAttrib)) { if (strcmp(node->Name(), CppcheckXml::RootPathName) == 0 && node->Attribute(CppcheckXml::RootPathNameAttrib)) {
temp.basePaths.push_back(joinRelativePath(path, node->Attribute(RootPathNameAttrib))); temp.basePaths.push_back(joinRelativePath(path, node->Attribute(CppcheckXml::RootPathNameAttrib)));
temp.relativePaths = true; temp.relativePaths = true;
} else if (strcmp(node->Name(), BuildDirElementName) == 0) } else if (strcmp(node->Name(), CppcheckXml::BuildDirElementName) == 0)
temp.buildDir = joinRelativePath(path, node->GetText() ? node->GetText() : ""); temp.buildDir = joinRelativePath(path, node->GetText() ? node->GetText() : "");
else if (strcmp(node->Name(), IncludeDirElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::IncludeDirElementName) == 0)
temp.includePaths = readXmlStringList(node, path, DirElementName, DirNameAttrib); temp.includePaths = readXmlStringList(node, path, CppcheckXml::DirElementName, CppcheckXml::DirNameAttrib);
else if (strcmp(node->Name(), DefinesElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::DefinesElementName) == 0)
temp.userDefines = join(readXmlStringList(node, "", DefineName, DefineNameAttrib), ";"); temp.userDefines = join(readXmlStringList(node, "", CppcheckXml::DefineName, CppcheckXml::DefineNameAttrib), ";");
else if (strcmp(node->Name(), UndefinesElementName) == 0) { else if (strcmp(node->Name(), CppcheckXml::UndefinesElementName) == 0) {
for (const std::string &u : readXmlStringList(node, "", UndefineName, nullptr)) for (const std::string &u : readXmlStringList(node, "", CppcheckXml::UndefineName, nullptr))
temp.userUndefs.insert(u); temp.userUndefs.insert(u);
} else if (strcmp(node->Name(), ImportProjectElementName) == 0) } else if (strcmp(node->Name(), CppcheckXml::ImportProjectElementName) == 0)
guiProject.projectFile = path + (node->GetText() ? node->GetText() : ""); guiProject.projectFile = path + (node->GetText() ? node->GetText() : "");
else if (strcmp(node->Name(), PathsElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::PathsElementName) == 0)
paths = readXmlStringList(node, path, PathName, PathNameAttrib); paths = readXmlStringList(node, path, CppcheckXml::PathName, CppcheckXml::PathNameAttrib);
else if (strcmp(node->Name(), ExcludeElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::ExcludeElementName) == 0)
guiProject.excludedPaths = readXmlStringList(node, "", ExcludePathName, ExcludePathNameAttrib); guiProject.excludedPaths = readXmlStringList(node, "", CppcheckXml::ExcludePathName, CppcheckXml::ExcludePathNameAttrib);
else if (strcmp(node->Name(), IgnoreElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::IgnoreElementName) == 0)
guiProject.excludedPaths = readXmlStringList(node, "", IgnorePathName, IgnorePathNameAttrib); guiProject.excludedPaths = readXmlStringList(node, "", CppcheckXml::IgnorePathName, CppcheckXml::IgnorePathNameAttrib);
else if (strcmp(node->Name(), LibrariesElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::LibrariesElementName) == 0)
guiProject.libraries = readXmlStringList(node, "", LibraryElementName, nullptr); guiProject.libraries = readXmlStringList(node, "", CppcheckXml::LibraryElementName, nullptr);
else if (strcmp(node->Name(), SuppressionsElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::SuppressionsElementName) == 0)
suppressions = readXmlStringList(node, "", SuppressionElementName, nullptr); suppressions = readXmlStringList(node, "", CppcheckXml::SuppressionElementName, nullptr);
else if (strcmp(node->Name(), PlatformElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::PlatformElementName) == 0)
guiProject.platform = node->GetText(); guiProject.platform = node->GetText();
else if (strcmp(node->Name(), AnalyzeAllVsConfigsElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::AnalyzeAllVsConfigsElementName) == 0)
guiProject.analyzeAllVsConfigs = node->GetText(); guiProject.analyzeAllVsConfigs = node->GetText();
else if (strcmp(node->Name(), AddonsElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::AddonsElementName) == 0)
temp.addons = readXmlStringList(node, "", AddonElementName, nullptr); temp.addons = readXmlStringList(node, "", CppcheckXml::AddonElementName, nullptr);
else if (strcmp(node->Name(), TagsElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::TagsElementName) == 0)
node->Attribute(TagElementName); // FIXME: Write some warning node->Attribute(CppcheckXml::TagElementName); // FIXME: Write some warning
else if (strcmp(node->Name(), ToolsElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::ToolsElementName) == 0)
node->Attribute(ToolElementName); // FIXME: Write some warning node->Attribute(CppcheckXml::ToolElementName); // FIXME: Write some warning
else if (strcmp(node->Name(), CheckHeadersElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::CheckHeadersElementName) == 0)
temp.checkHeaders = (strcmp(node->GetText(), "true") == 0); temp.checkHeaders = (strcmp(node->GetText(), "true") == 0);
else if (strcmp(node->Name(), CheckUnusedTemplatesElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::CheckUnusedTemplatesElementName) == 0)
temp.checkUnusedTemplates = (strcmp(node->GetText(), "true") == 0); temp.checkUnusedTemplates = (strcmp(node->GetText(), "true") == 0);
else if (strcmp(node->Name(), MaxCtuDepthElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::MaxCtuDepthElementName) == 0)
temp.maxCtuDepth = std::atoi(node->GetText()); temp.maxCtuDepth = std::atoi(node->GetText());
else if (strcmp(node->Name(), CppcheckXml::CheckUnknownFunctionReturn) == 0)
; // TODO
else if (strcmp(node->Name(), Settings::SafeChecks::XmlRootName) == 0) { else if (strcmp(node->Name(), Settings::SafeChecks::XmlRootName) == 0) {
for (const tinyxml2::XMLElement *child = node->FirstChildElement(); child; child = child->NextSiblingElement()) { for (const tinyxml2::XMLElement *child = node->FirstChildElement(); child; child = child->NextSiblingElement()) {
if (strcmp(child->Name(), Settings::SafeChecks::XmlClasses) == 0) if (strcmp(child->Name(), Settings::SafeChecks::XmlClasses) == 0)

View File

@ -110,6 +110,51 @@ private:
std::string mPath; std::string mPath;
}; };
namespace CppcheckXml {
constexpr char ProjectElementName[] = "project";
constexpr char ProjectVersionAttrib[] = "version";
constexpr char ProjectFileVersion[] = "1";
constexpr char BuildDirElementName[] = "builddir";
constexpr char ImportProjectElementName[] = "importproject";
constexpr char AnalyzeAllVsConfigsElementName[] = "analyze-all-vs-configs";
constexpr char IncludeDirElementName[] = "includedir";
constexpr char DirElementName[] = "dir";
constexpr char DirNameAttrib[] = "name";
constexpr char DefinesElementName[] = "defines";
constexpr char DefineName[] = "define";
constexpr char DefineNameAttrib[] = "name";
constexpr char UndefinesElementName[] = "undefines";
constexpr char UndefineName[] = "undefine";
constexpr char PathsElementName[] = "paths";
constexpr char PathName[] = "dir";
constexpr char PathNameAttrib[] = "name";
constexpr char RootPathName[] = "root";
constexpr char RootPathNameAttrib[] = "name";
constexpr char IgnoreElementName[] = "ignore";
constexpr char IgnorePathName[] = "path";
constexpr char IgnorePathNameAttrib[] = "name";
constexpr char ExcludeElementName[] = "exclude";
constexpr char ExcludePathName[] = "path";
constexpr char ExcludePathNameAttrib[] = "name";
constexpr char LibrariesElementName[] = "libraries";
constexpr char LibraryElementName[] = "library";
constexpr char PlatformElementName[] = "platform";
constexpr char SuppressionsElementName[] = "suppressions";
constexpr char SuppressionElementName[] = "suppression";
constexpr char AddonElementName[] = "addon";
constexpr char AddonsElementName[] = "addons";
constexpr char ToolElementName[] = "tool";
constexpr char ToolsElementName[] = "tools";
constexpr char TagsElementName[] = "tags";
constexpr char TagElementName[] = "tag";
constexpr char CheckHeadersElementName[] = "check-headers";
constexpr char CheckUnusedTemplatesElementName[] = "check-unused-templates";
constexpr char MaxCtuDepthElementName[] = "max-ctu-depth";
constexpr char CheckUnknownFunctionReturn[] = "check-unknown-function-return-values";
constexpr char Name[] = "name";
}
/// @} /// @}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#endif // importprojectH #endif // importprojectH