cppcheck/gui/projectfile.cpp

691 lines
22 KiB
C++
Raw Normal View History

/*
* Cppcheck - A tool for static C/C++ code analysis
2018-01-14 15:37:52 +01:00
* Copyright (C) 2007-2017 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"
static const char ProjectElementName[] = "project";
static const char ProjectVersionAttrib[] = "version";
static const char ProjectFileVersion[] = "1";
2016-11-19 20:38:50 +01:00
static const char BuildDirElementName[] = "builddir";
2016-08-18 21:58:50 +02:00
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 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";
2013-12-29 18:06:31 +01:00
static const char LibrariesElementName[] = "libraries";
static const char LibraryElementName[] = "library";
static const char PlatformElementName[] = "platform";
2013-12-30 22:32:50 +01:00
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";
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()
{
mRootPath.clear();
mBuildDir.clear();
mImportProject.clear();
mAnalyzeAllVsConfigs = true;
mIncludeDirs.clear();
mDefines.clear();
mPaths.clear();
mExcludedPaths.clear();
mLibraries.clear();
mPlatform.clear();
mSuppressions.clear();
mAddons.clear();
mClangAnalyzer = mClangTidy = false;
}
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:
2011-10-13 20:53:06 +02:00
if (xmlReader.name() == ProjectElementName) {
insideProject = true;
projectTagFound = true;
}
// Read root path from inside project element
if (insideProject && xmlReader.name() == 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 (insideProject && xmlReader.name() == 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 (insideProject && xmlReader.name() == PathsElementName)
2017-07-28 05:08:59 +02:00
readCheckPaths(xmlReader);
2016-08-18 21:58:50 +02:00
if (insideProject && xmlReader.name() == ImportProjectElementName)
2017-07-28 05:08:59 +02:00
readImportProject(xmlReader);
2016-08-18 21:58:50 +02:00
if (insideProject && xmlReader.name() == AnalyzeAllVsConfigsElementName)
readAnalyzeAllVsConfigs(xmlReader);
// Find include directory from inside project element
if (insideProject && xmlReader.name() == IncludeDirElementName)
2017-07-28 05:08:59 +02:00
readIncludeDirs(xmlReader);
// Find preprocessor define from inside project element
if (insideProject && xmlReader.name() == DefinesElementName)
2017-07-28 05:08:59 +02:00
readDefines(xmlReader);
// Find exclude list from inside project element
if (insideProject && xmlReader.name() == ExcludeElementName)
2017-07-28 05:08:59 +02:00
readExcludes(xmlReader);
// Find ignore list from inside project element
// These are read for compatibility
if (insideProject && xmlReader.name() == IgnoreElementName)
2017-07-28 05:08:59 +02:00
readExcludes(xmlReader);
2013-12-30 22:32:50 +01:00
// Find libraries list from inside project element
2013-12-29 18:06:31 +01:00
if (insideProject && xmlReader.name() == LibrariesElementName)
2017-07-28 05:08:59 +02:00
readStringList(mLibraries, xmlReader,LibraryElementName);
2013-12-30 22:32:50 +01:00
if (insideProject && xmlReader.name() == PlatformElementName)
readPlatform(xmlReader);
2013-12-30 22:32:50 +01:00
// Find suppressions list from inside project element
if (insideProject && xmlReader.name() == SuppressionsElementName)
2017-07-28 05:08:59 +02:00
readStringList(mSuppressions, xmlReader,SuppressionElementName);
2013-12-29 18:06:31 +01:00
// Addons
if (insideProject && xmlReader.name() == AddonsElementName)
readStringList(mAddons, xmlReader, AddonElementName);
// Tools
if (insideProject && xmlReader.name() == ToolsElementName) {
QStringList tools;
readStringList(tools, xmlReader, ToolElementName);
mClangAnalyzer = tools.contains(CLANG_ANALYZER);
mClangTidy = tools.contains(CLANG_TIDY);
}
if (insideProject && xmlReader.name() == TagsElementName)
readStringList(mTags, xmlReader, TagElementName);
break;
case QXmlStreamReader::EndElement:
if (xmlReader.name() == 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();
2017-08-03 12:39:31 +02:00
QString name = attribs.value(QString(), 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 (1);
}
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 (1);
}
void ProjectFile::readAnalyzeAllVsConfigs(QXmlStreamReader &reader)
{
do {
const QXmlStreamReader::TokenType type = reader.readNext();
switch (type) {
case QXmlStreamReader::Characters:
mAnalyzeAllVsConfigs = (reader.text().toString() == "true");
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 (1);
}
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
2011-10-13 20:53:06 +02:00
if (reader.name().toString() == DirElementName) {
QXmlStreamAttributes attribs = reader.attributes();
2017-08-03 12:39:31 +02:00
QString name = attribs.value(QString(), DirNameAttrib).toString();
if (!name.isEmpty())
mIncludeDirs << name;
}
break;
case QXmlStreamReader::EndElement:
if (reader.name().toString() == 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
2011-10-13 20:53:06 +02:00
if (reader.name().toString() == DefineName) {
QXmlStreamAttributes attribs = reader.attributes();
2017-08-03 12:39:31 +02:00
QString name = attribs.value(QString(), DefineNameAttrib).toString();
if (!name.isEmpty())
mDefines << name;
}
break;
case QXmlStreamReader::EndElement:
if (reader.name().toString() == 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
2011-10-13 20:53:06 +02:00
if (reader.name().toString() == PathName) {
QXmlStreamAttributes attribs = reader.attributes();
2017-08-03 12:39:31 +02:00
QString name = attribs.value(QString(), PathNameAttrib).toString();
if (!name.isEmpty())
mPaths << name;
}
break;
case QXmlStreamReader::EndElement:
if (reader.name().toString() == 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
2011-10-13 20:53:06 +02:00
if (reader.name().toString() == ExcludePathName) {
QXmlStreamAttributes attribs = reader.attributes();
2017-08-03 12:39:31 +02:00
QString name = attribs.value(QString(), ExcludePathNameAttrib).toString();
if (!name.isEmpty())
mExcludedPaths << name;
}
// Read ignore-elements - deprecated but support reading them
2011-10-13 20:53:06 +02:00
else if (reader.name().toString() == IgnorePathName) {
QXmlStreamAttributes attribs = reader.attributes();
2017-08-03 12:39:31 +02:00
QString name = attribs.value(QString(), IgnorePathNameAttrib).toString();
if (!name.isEmpty())
mExcludedPaths << name;
}
break;
case QXmlStreamReader::EndElement:
if (reader.name().toString() == IgnoreElementName)
allRead = true;
if (reader.name().toString() == 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::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 (1);
}
2013-12-29 18:06:31 +01:00
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;
}
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::setPlatform(const QString &platform)
{
mPlatform = platform;
}
2017-07-28 05:08:59 +02:00
void ProjectFile::setSuppressions(const QStringList &suppressions)
2013-12-30 22:32:50 +01:00
{
mSuppressions = suppressions;
}
void ProjectFile::setAddons(const QStringList &addons)
{
mAddons = addons;
}
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(ProjectElementName);
xmlWriter.writeAttribute(ProjectVersionAttrib, ProjectFileVersion);
2011-10-13 20:53:06 +02:00
if (!mRootPath.isEmpty()) {
xmlWriter.writeStartElement(RootPathName);
xmlWriter.writeAttribute(RootPathNameAttrib, mRootPath);
xmlWriter.writeEndElement();
}
2016-11-19 20:38:50 +01:00
if (!mBuildDir.isEmpty()) {
xmlWriter.writeStartElement(BuildDirElementName);
xmlWriter.writeCharacters(mBuildDir);
xmlWriter.writeEndElement();
}
if (!mPlatform.isEmpty()) {
xmlWriter.writeStartElement(PlatformElementName);
xmlWriter.writeCharacters(mPlatform);
xmlWriter.writeEndElement();
}
2016-08-18 21:58:50 +02:00
if (!mImportProject.isEmpty()) {
xmlWriter.writeStartElement(ImportProjectElementName);
xmlWriter.writeCharacters(mImportProject);
xmlWriter.writeEndElement();
}
xmlWriter.writeStartElement(AnalyzeAllVsConfigsElementName);
xmlWriter.writeCharacters(mAnalyzeAllVsConfigs ? "true" : "false");
xmlWriter.writeEndElement();
2011-10-13 20:53:06 +02:00
if (!mIncludeDirs.isEmpty()) {
xmlWriter.writeStartElement(IncludeDirElementName);
foreach (QString incdir, mIncludeDirs) {
xmlWriter.writeStartElement(DirElementName);
xmlWriter.writeAttribute(DirNameAttrib, incdir);
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
}
2011-10-13 20:53:06 +02:00
if (!mDefines.isEmpty()) {
xmlWriter.writeStartElement(DefinesElementName);
foreach (QString define, mDefines) {
xmlWriter.writeStartElement(DefineName);
xmlWriter.writeAttribute(DefineNameAttrib, define);
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
}
2011-10-13 20:53:06 +02:00
if (!mPaths.isEmpty()) {
xmlWriter.writeStartElement(PathsElementName);
foreach (QString path, mPaths) {
xmlWriter.writeStartElement(PathName);
xmlWriter.writeAttribute(PathNameAttrib, path);
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
}
2011-10-13 20:53:06 +02:00
if (!mExcludedPaths.isEmpty()) {
xmlWriter.writeStartElement(ExcludeElementName);
foreach (QString path, mExcludedPaths) {
xmlWriter.writeStartElement(ExcludePathName);
xmlWriter.writeAttribute(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,
LibrariesElementName,
LibraryElementName);
2017-07-28 05:08:59 +02:00
writeStringList(xmlWriter,
2013-12-30 22:32:50 +01:00
mSuppressions,
SuppressionsElementName,
SuppressionElementName);
2013-12-29 18:06:31 +01:00
writeStringList(xmlWriter,
mAddons,
AddonsElementName,
AddonElementName);
QStringList tools;
if (mClangAnalyzer)
tools << CLANG_ANALYZER;
if (mClangTidy)
tools << CLANG_TIDY;
writeStringList(xmlWriter,
tools,
ToolsElementName,
ToolElementName);
writeStringList(xmlWriter, mTags, TagsElementName, TagElementName);
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;
}