Commit 602ad72d authored by Nikolai Kosjar's avatar Nikolai Kosjar

CppTools: Refactor ProjectPartBuilder

...and add some basic tests.

Introduce the abstractions ProjectInterface and ToolChainInterface in
order to break the dependency to the ProjectExplorer. Also, some simple
logic can go there to simplify the (Base)ProjectPartBuilder.

Change-Id: I6c50a1804ce62098b87109931eb171f5c2542937
Reviewed-by: David Schulz's avatarDavid Schulz <david.schulz@qt.io>
parent 95fa59dd
This diff is collapsed.
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "cpptools_global.h"
#include "cppprojectinterface.h"
#include "projectpart.h"
#include <projectexplorer/toolchain.h>
#include <functional>
#include <memory>
namespace CppTools {
class ProjectInfo;
class CPPTOOLS_EXPORT BaseProjectPartBuilder
{
public:
BaseProjectPartBuilder(ProjectInterface *project, ProjectInfo &projectInfo);
void setDisplayName(const QString &displayName);
void setProjectFile(const QString &projectFile);
void setConfigFileName(const QString &configFileName);
void setQtVersion(ProjectPart::QtVersion qtVersion);
void setCFlags(const QStringList &flags);
void setCxxFlags(const QStringList &flags);
void setDefines(const QByteArray &defines);
void setHeaderPaths(const ProjectPartHeaderPaths &headerPaths);
void setIncludePaths(const QStringList &includePaths);
void setPreCompiledHeaders(const QStringList &preCompiledHeaders);
using FileClassifier = std::function<ProjectFile::Kind (const QString &filePath)>;
QList<Core::Id> createProjectPartsForFiles(const QStringList &filePaths,
FileClassifier fileClassifier = FileClassifier());
static void evaluateToolChain(ProjectPart &projectPart,
const ToolChainInterface &selectToolChain);
private:
void createProjectPart(const ProjectFiles &projectFiles,
const QString &partName,
ProjectPart::LanguageVersion languageVersion,
ProjectPart::LanguageExtensions languageExtensions);
ToolChainInterfacePtr selectToolChain(ProjectPart::LanguageVersion languageVersion);
private:
std::unique_ptr<ProjectInterface> m_project;
ProjectInfo &m_projectInfo;
ProjectPart::Ptr m_templatePart;
QStringList m_cFlags;
QStringList m_cxxFlags;
};
} // namespace CppTools
......@@ -39,6 +39,12 @@ ProjectFile::ProjectFile(const QString &filePath, Kind kind)
{
}
bool ProjectFile::operator==(const ProjectFile &other) const
{
return path == other.path
&& kind == other.kind;
}
ProjectFile::Kind ProjectFile::classify(const QString &filePath)
{
if (isAmbiguousHeader(filePath))
......
......@@ -59,6 +59,8 @@ public:
ProjectFile() = default;
ProjectFile(const QString &filePath, Kind kind);
bool operator==(const ProjectFile &other) const;
QString path;
Kind kind = Unclassified;
};
......
......@@ -41,6 +41,7 @@ ProjectFileCategorizer::ProjectFileCategorizer(const QString &projectPartName,
+ (m_objcxxSources.isEmpty() ? 0 : 1);
}
// TODO: Always tell the language version?
QString ProjectFileCategorizer::partName(const QString &languageName) const
{
if (hasMultipleParts())
......
......@@ -54,7 +54,7 @@ public:
ProjectFiles objcxxSources() const { return m_objcxxSources; }
bool hasMultipleParts() const { return m_partCount > 1; }
bool hasNoParts() const { return m_partCount == 0; }
bool hasParts() const { return m_partCount > 0; }
QString partName(const QString &languageName) const;
......
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include <projectexplorer/toolchain.h>
#include <memory>
QT_BEGIN_NAMESPACE
class QString;
QT_END_NAMESPACE
namespace Core {
class Id;
}
namespace ProjectExplorer {
class Project;
class ToolChain;
}
namespace CppTools {
class ToolChainInterface
{
public:
virtual ~ToolChainInterface() {}
virtual Core::Id type() const = 0;
virtual bool isMsvc2015Toolchain() const = 0;
virtual unsigned wordWidth() const = 0;
virtual QString targetTriple() const = 0;
virtual QByteArray predefinedMacros() const = 0;
virtual QList<ProjectExplorer::HeaderPath> systemHeaderPaths() const = 0;
virtual ProjectExplorer::WarningFlags warningFlags() const = 0;
virtual ProjectExplorer::ToolChain::CompilerFlags compilerFlags() const = 0;
};
using ToolChainInterfacePtr = std::unique_ptr<ToolChainInterface>;
class ProjectInterface
{
public:
virtual ~ProjectInterface() {}
virtual QString displayName() const = 0;
virtual QString projectFilePath() const = 0;
virtual ToolChainInterfacePtr toolChain(ProjectExplorer::ToolChain::Language language,
const QStringList &commandLineFlags) const = 0;
};
} // namespace CppTools
......@@ -78,7 +78,9 @@ HEADERS += \
projectpartbuilder.h \
compileroptionsbuilder.h \
refactoringengineinterface.h \
cppprojectfilecategorizer.h
cppprojectfilecategorizer.h \
cppprojectinterface.h \
cppbaseprojectpartbuilder.h \
SOURCES += \
abstracteditorsupport.cpp \
......@@ -151,7 +153,8 @@ SOURCES += \
projectinfo.cpp \
projectpartbuilder.cpp \
compileroptionsbuilder.cpp \
cppprojectfilecategorizer.cpp
cppprojectfilecategorizer.cpp \
cppbaseprojectpartbuilder.cpp \
FORMS += \
clangdiagnosticconfigswidget.ui \
......
......@@ -105,6 +105,8 @@ Project {
"symbolsfindfilter.cpp", "symbolsfindfilter.h",
"typehierarchybuilder.cpp", "typehierarchybuilder.h",
"cppprojectfilecategorizer.cpp", "cppprojectfilecategorizer.h",
"cppprojectinterface.h",
"cppbaseprojectpartbuilder.cpp", "cppbaseprojectpartbuilder.h",
]
Group {
......
# Currently there are no tests for the project explorer plugin, but we include
# headers from it that needs to have the export/import adapted for Windows.
contains(CONFIG, dll) {
DEFINES += CPPTOOLS_LIBRARY
DEFINES += PROJECTEXPLORER_LIBRARY
} else {
DEFINES += CPPTOOLS_STATIC_LIBRARY
DEFINES += PROJECTEXPLORER_STATIC_LIBRARY
}
HEADERS += \
......@@ -9,11 +13,17 @@ HEADERS += \
$$PWD/senddocumenttracker.h \
$$PWD/projectpart.h \
$$PWD/compileroptionsbuilder.h \
$$PWD/cppprojectfilecategorizer.h
$$PWD/cppprojectfilecategorizer.h \
$$PWD/cppbaseprojectpartbuilder.h \
$$PWD/projectinfo.h \
$$PWD/cppprojectinterface.h \
SOURCES += \
$$PWD/cppprojectfile.cpp \
$$PWD/senddocumenttracker.cpp \
$$PWD/projectpart.cpp \
$$PWD/compileroptionsbuilder.cpp \
$$PWD/cppprojectfilecategorizer.cpp
$$PWD/cppprojectfilecategorizer.cpp \
$$PWD/cppbaseprojectpartbuilder.cpp \
$$PWD/projectinfo.cpp \
......@@ -58,7 +58,7 @@ public: // Types
CXX03,
CXX11,
CXX14,
CXX17
CXX17,
};
enum LanguageExtension {
......
This diff is collapsed.
......@@ -27,53 +27,26 @@
#include "cpptools_global.h"
#include "projectinfo.h"
#include "projectpart.h"
#include <functional>
#include "cppbaseprojectpartbuilder.h"
namespace ProjectExplorer {
class Kit;
class ToolChain;
}
namespace CppTools {
class CPPTOOLS_EXPORT ProjectPartBuilder
class ProjectInfo;
class CPPTOOLS_EXPORT ProjectPartBuilder : public BaseProjectPartBuilder
{
public:
ProjectPartBuilder(ProjectInfo &m_pInfo);
void setQtVersion(ProjectPart::QtVersion qtVersion);
void setCFlags(const QStringList &flags);
void setCxxFlags(const QStringList &flags);
void setDefines(const QByteArray &defines);
void setHeaderPaths(const ProjectPartHeaderPaths &headerPaths);
void setIncludePaths(const QStringList &includePaths);
void setPreCompiledHeaders(const QStringList &pchs);
void setProjectFile(const QString &projectFile);
void setDisplayName(const QString &displayName);
void setConfigFileName(const QString &configFileName);
using FileClassifier = std::function<ProjectFile::Kind (const QString &filePath)>;
QList<Core::Id> createProjectPartsForFiles(const QStringList &files,
FileClassifier fileClassifier = FileClassifier());
static void evaluateProjectPartToolchain(ProjectPart *projectPart,
const ProjectExplorer::ToolChain *toolChain,
const QStringList &commandLineFlags,
const Utils::FileName &sysRoot);
private:
void createProjectPart(const QVector<ProjectFile> &theSources,
const QString &partName,
ProjectPart::LanguageVersion languageVersion,
ProjectPart::LanguageExtensions languageExtensions);
ProjectPartBuilder(ProjectInfo &projectInfo);
private:
ProjectPart::Ptr m_templatePart;
ProjectInfo &m_pInfo;
QStringList m_cFlags, m_cxxFlags;
static void evaluateToolChain(ProjectPart &projectPart,
ProjectExplorer::ToolChain &toolChain,
const ProjectExplorer::Kit *kit,
const QStringList commandLineFlags);
};
} // namespace CppTools
......@@ -29,6 +29,8 @@
#if defined(PROJECTEXPLORER_LIBRARY)
# define PROJECTEXPLORER_EXPORT Q_DECL_EXPORT
#elif defined(PROJECTEXPLORER_STATIC_LIBRARY)
# define PROJECTEXPLORER_EXPORT
#else
# define PROJECTEXPLORER_EXPORT Q_DECL_IMPORT
#endif
......@@ -413,9 +413,8 @@ void QmakeProject::updateCppCodeModel()
// TODO: there is no LANG_OBJCXX, so:
const QStringList cxxflags = pro->variableValue(CppFlagsVar);
CppTools::ProjectPartBuilder::evaluateProjectPartToolchain(
templatePart.data(), ToolChainKitInformation::toolChain(k, ToolChain::Language::Cxx),
cxxflags, SysRootKitInformation::sysRoot(k));
if (ToolChain *t = ToolChainKitInformation::toolChain(k, ToolChain::Language::Cxx))
CppTools::ProjectPartBuilder::evaluateToolChain(*templatePart.data(), *t, k, cxxflags);
setProjectLanguage(ProjectExplorer::Constants::LANG_CXX, true);
ProjectPart::Ptr cppPart = templatePart->copy();
......
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "googletest.h"
#include "gtest-qt-printing.h"
#include "mimedatabase-utilities.h"
#include <cpptools/cppprojectfilecategorizer.h>
#include <cpptools/cppbaseprojectpartbuilder.h>
#include <cpptools/cppprojectinterface.h>
#include <cpptools/projectinfo.h>
#include <projectexplorer/headerpath.h>
#include <projectexplorer/project.h>
#include <utils/mimetypes/mimedatabase.h>
#include <QObject>
using CppTools::BaseProjectPartBuilder;
using CppTools::ProjectFile;
using CppTools::ProjectFiles;
using CppTools::ProjectInfo;
using CppTools::ProjectInterface;
using CppTools::ProjectPart;
using CppTools::ToolChainInterface;
using CppTools::ToolChainInterfacePtr;
using testing::Eq;
namespace {
class EditableToolChain : public CppTools::ToolChainInterface
{
public:
void setCompilerFlags(ProjectExplorer::ToolChain::CompilerFlags compilerFlags)
{
m_compilerFlags = compilerFlags;
}
private:
Core::Id type() const override { return Core::Id(); }
bool isMsvc2015Toolchain() const override { return false; }
unsigned wordWidth() const override { return 64; }
QString targetTriple() const override { return QString(); }
QByteArray predefinedMacros() const override { return QByteArray(); }
QList<ProjectExplorer::HeaderPath> systemHeaderPaths() const override
{ return QList<ProjectExplorer::HeaderPath>(); }
ProjectExplorer::WarningFlags warningFlags() const override
{ return ProjectExplorer::WarningFlags(); }
ProjectExplorer::ToolChain::CompilerFlags compilerFlags() const override
{ return m_compilerFlags; }
private:
ProjectExplorer::ToolChain::CompilerFlags m_compilerFlags;
};
class EditableProject : public CppTools::ProjectInterface
{
public:
void setToolChain(ToolChainInterface *toolChain)
{
m_toolChain = toolChain;
}
private:
QString displayName() const override { return QString(); }
QString projectFilePath() const override { return QString(); }
ToolChainInterfacePtr toolChain(ProjectExplorer::ToolChain::Language,
const QStringList &) const override
{ return ToolChainInterfacePtr(m_toolChain); }
private:
CppTools::ToolChainInterface *m_toolChain = nullptr;
};
class BaseProjectPartBuilder : public ::testing::Test
{
protected:
void SetUp() override;
QObject dummyProjectExplorerProject;
ProjectInfo projectInfo{static_cast<ProjectExplorer::Project *>(&dummyProjectExplorerProject)};
};
TEST_F(BaseProjectPartBuilder, CreateNoPartsForEmptyFileList)
{
::BaseProjectPartBuilder builder(new EditableProject, projectInfo);
builder.createProjectPartsForFiles(QStringList());
ASSERT_TRUE(projectInfo.projectParts().isEmpty());
}
TEST_F(BaseProjectPartBuilder, CreateSinglePart)
{
::BaseProjectPartBuilder builder(new EditableProject, projectInfo);
builder.createProjectPartsForFiles(QStringList() << "foo.cpp" << "foo.h");
ASSERT_THAT(projectInfo.projectParts().size(), Eq(1));
}
TEST_F(BaseProjectPartBuilder, CreateMultipleParts)
{
::BaseProjectPartBuilder builder(new EditableProject, projectInfo);
builder.createProjectPartsForFiles(QStringList() << "foo.cpp" << "foo.h"
<< "bar.c" << "bar.h");
ASSERT_THAT(projectInfo.projectParts().size(), Eq(2));
}
TEST_F(BaseProjectPartBuilder, ProjectPartIndicatesObjectiveCExtensionsByDefault)
{
::BaseProjectPartBuilder builder(new EditableProject, projectInfo);
builder.createProjectPartsForFiles(QStringList() << "foo.mm");
ASSERT_THAT(projectInfo.projectParts().size(), Eq(1));
const ProjectPart &projectPart = *projectInfo.projectParts().at(0);
ASSERT_TRUE(projectPart.languageExtensions & ProjectPart::ObjectiveCExtensions);
}
TEST_F(BaseProjectPartBuilder, ProjectPartHasLatestLanguageVersionByDefault)
{
::BaseProjectPartBuilder builder(new EditableProject, projectInfo);
builder.createProjectPartsForFiles(QStringList() << "foo.cpp");
ASSERT_THAT(projectInfo.projectParts().size(), Eq(1));
const ProjectPart &projectPart = *projectInfo.projectParts().at(0);
ASSERT_THAT(projectPart.languageVersion, Eq(ProjectPart::CXX11));
}
TEST_F(BaseProjectPartBuilder, ToolChainSetsLanguageVersion)
{
auto toolChain = new EditableToolChain;
toolChain->setCompilerFlags(ProjectExplorer::ToolChain::StandardCxx98);
auto project = new EditableProject;
project->setToolChain(toolChain);
::BaseProjectPartBuilder builder(project, projectInfo);
builder.createProjectPartsForFiles(QStringList() << "foo.cpp");
ASSERT_THAT(projectInfo.projectParts().size(), Eq(1));
const ProjectPart &projectPart = *projectInfo.projectParts().at(0);
ASSERT_THAT(projectPart.languageVersion, Eq(ProjectPart::CXX98));
}
TEST_F(BaseProjectPartBuilder, ToolChainSetsLanguageExtensions)
{
auto toolChain = new EditableToolChain;
toolChain->setCompilerFlags(ProjectExplorer::ToolChain::MicrosoftExtensions);
auto project = new EditableProject;
project->setToolChain(toolChain);
::BaseProjectPartBuilder builder(project, projectInfo);
builder.createProjectPartsForFiles(QStringList() << "foo.cpp");
ASSERT_THAT(projectInfo.projectParts().size(), Eq(1));
const ProjectPart &projectPart = *projectInfo.projectParts().at(0);
ASSERT_TRUE(projectPart.languageExtensions & ProjectPart::MicrosoftExtensions);
}
TEST_F(BaseProjectPartBuilder, ProjectFileKindsMatchProjectPartVersion)
{
::BaseProjectPartBuilder builder(new EditableProject, projectInfo);
builder.createProjectPartsForFiles(QStringList() << "foo.h");
const QList<ProjectPart::Ptr> projectParts = projectInfo.projectParts();
ASSERT_THAT(projectParts.size(), Eq(4));
ASSERT_THAT(projectParts.at(0)->languageVersion, Eq(ProjectPart::C11));
ASSERT_THAT(projectParts.at(0)->files.at(0).kind, Eq(ProjectFile::CHeader));
ASSERT_THAT(projectParts.at(1)->languageVersion, Eq(ProjectPart::C11));
ASSERT_THAT(projectParts.at(1)->files.at(0).kind, Eq(ProjectFile::ObjCHeader));
ASSERT_THAT(projectParts.at(2)->languageVersion, Eq(ProjectPart::CXX11));
ASSERT_THAT(projectParts.at(2)->files.at(0).kind, Eq(ProjectFile::CXXHeader));
ASSERT_THAT(projectParts.at(3)->languageVersion, Eq(ProjectPart::CXX11));
ASSERT_THAT(projectParts.at(3)->files.at(0).kind, Eq(ProjectFile::ObjCXXHeader));
}
void BaseProjectPartBuilder::SetUp()
{
ASSERT_TRUE(MimeDataBaseUtilities::addCppToolsMimeTypes());
}
} // anonymous namespace
......@@ -25,6 +25,7 @@
#include "googletest.h"
#include "gtest-qt-printing.h"
#include "mimedatabase-utilities.h"
#include <cpptools/cppprojectfilecategorizer.h>
#include <utils/mimetypes/mimedatabase.h>
......@@ -46,12 +47,6 @@ using testing::AllOf;
namespace CppTools {
bool operator==(const ProjectFile &lhs, const ProjectFile &rhs)
{
return lhs.path == rhs.path
&& lhs.kind == rhs.kind;
}
void PrintTo(const ProjectFile &projectFile, std::ostream *os)
{
*os << "ProjectFile(";
......@@ -71,7 +66,6 @@ protected:
void SetUp() override;
static ProjectFiles singleFile(const QString &filePath, ProjectFile::Kind kind);
static void initMimeDataBaseIfNotYetDone();
protected:
const QString dummyProjectPartName;
......@@ -192,7 +186,7 @@ TEST_F(ProjectFileCategorizer, AmbiguousHeaderOnly)
void ProjectFileCategorizer::SetUp()
{
initMimeDataBaseIfNotYetDone();
ASSERT_TRUE(MimeDataBaseUtilities::addCppToolsMimeTypes());
}
QVector<CppTools::ProjectFile>ProjectFileCategorizer::singleFile(const QString &filePath,
......@@ -201,16 +195,4 @@ QVector<CppTools::ProjectFile>ProjectFileCategorizer::singleFile(const QString &
return { ProjectFile(filePath, kind) };