Commit 876ea282 authored by Erik Verbruggen's avatar Erik Verbruggen Committed by Nikolai Kosjar

GenericProject: put different languages into separate project parts.

This is in preparation for changes to the ProjectParts, where one part
can only hold files for 1 language.

Change-Id: I5e9a1a803ecc3ecbb36added0d0094df63922549
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent bb8575c6
...@@ -31,11 +31,7 @@ ...@@ -31,11 +31,7 @@
#include <cplusplus/pp-engine.h> #include <cplusplus/pp-engine.h>
#include <projectexplorer/headerpath.h>
#include <projectexplorer/toolchain.h>
using namespace CppTools; using namespace CppTools;
using namespace ProjectExplorer;
/*! /*!
\enum CppTools::CppModelManagerInterface::ProgressNotificationMode \enum CppTools::CppModelManagerInterface::ProgressNotificationMode
......
...@@ -30,11 +30,17 @@ ...@@ -30,11 +30,17 @@
#include "cppprojects.h" #include "cppprojects.h"
#include <projectexplorer/headerpath.h> #include <projectexplorer/headerpath.h>
#include <projectexplorer/kit.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/kitmanager.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/target.h>
#include <QSet> #include <QSet>
#include <QTextStream> #include <QTextStream>
namespace CppTools { using namespace CppTools;
using namespace ProjectExplorer;
ProjectPart::ProjectPart() ProjectPart::ProjectPart()
: project(0) : project(0)
...@@ -44,7 +50,6 @@ ProjectPart::ProjectPart() ...@@ -44,7 +50,6 @@ ProjectPart::ProjectPart()
, qtVersion(UnknownQt) , qtVersion(UnknownQt)
, cWarningFlags(ProjectExplorer::ToolChain::WarningsDefault) , cWarningFlags(ProjectExplorer::ToolChain::WarningsDefault)
, cxxWarningFlags(ProjectExplorer::ToolChain::WarningsDefault) , cxxWarningFlags(ProjectExplorer::ToolChain::WarningsDefault)
{ {
} }
...@@ -103,6 +108,11 @@ void ProjectPart::evaluateToolchain(const ProjectExplorer::ToolChain *tc, ...@@ -103,6 +108,11 @@ void ProjectPart::evaluateToolchain(const ProjectExplorer::ToolChain *tc,
toolchainDefines = tc->predefinedMacros(cxxflags); toolchainDefines = tc->predefinedMacros(cxxflags);
} }
ProjectPart::Ptr ProjectPart::copy() const
{
return Ptr(new ProjectPart(*this));
}
QByteArray ProjectPart::readProjectConfigFile(const ProjectPart::Ptr &part) QByteArray ProjectPart::readProjectConfigFile(const ProjectPart::Ptr &part)
{ {
QByteArray result; QByteArray result;
...@@ -208,4 +218,222 @@ const QByteArray ProjectInfo::defines() const ...@@ -208,4 +218,222 @@ const QByteArray ProjectInfo::defines() const
return m_defines; return m_defines;
} }
} // namespace CppTools namespace {
class ProjectFileCategorizer
{
public:
ProjectFileCategorizer(const QString &partName, const QStringList &files)
: m_partName(partName)
{
using CppTools::ProjectFile;
QStringList cHeaders, cxxHeaders;
foreach (const QString &file, files) {
switch (ProjectFile::classify(file)) {
case ProjectFile::CSource: m_cSources += file; break;
case ProjectFile::CHeader: cHeaders += file; break;
case ProjectFile::CXXSource: m_cxxSources += file; break;
case ProjectFile::CXXHeader: cxxHeaders += file; break;
case ProjectFile::ObjCSource: m_objcSources += file; break;
case ProjectFile::ObjCXXSource: m_objcxxSources += file; break;
default:
continue;
}
}
const bool hasC = !m_cSources.isEmpty();
const bool hasCxx = !m_cxxSources.isEmpty();
const bool hasObjc = !m_objcSources.isEmpty();
const bool hasObjcxx = !m_objcxxSources.isEmpty();
if (hasObjcxx)
m_objcxxSources += cxxHeaders + cHeaders;
if (hasCxx)
m_cxxSources += cxxHeaders + cHeaders;
else if (!hasObjcxx)
m_cxxSources += cxxHeaders;
if (hasObjc)
m_objcSources += cHeaders;
if (hasC || (!hasObjc && !hasObjcxx && !hasCxx))
m_cSources += cHeaders;
m_partCount =
(m_cSources.isEmpty() ? 0 : 1) +
(m_cxxSources.isEmpty() ? 0 : 1) +
(m_objcSources.isEmpty() ? 0 : 1) +
(m_objcxxSources.isEmpty() ? 0 : 1);
}
bool hasCSources() const { return !m_cSources.isEmpty(); }
bool hasCxxSources() const { return !m_cxxSources.isEmpty(); }
bool hasObjcSources() const { return !m_objcSources.isEmpty(); }
bool hasObjcxxSources() const { return !m_objcxxSources.isEmpty(); }
QStringList cSources() const { return m_cSources; }
QStringList cxxSources() const { return m_cxxSources; }
QStringList objcSources() const { return m_objcSources; }
QStringList objcxxSources() const { return m_objcxxSources; }
bool hasMultipleParts() const { return m_partCount > 1; }
bool hasNoParts() const { return m_partCount == 0; }
QString partName(const QString &languageName) const
{
if (hasMultipleParts())
return QString::fromLatin1("%1 (%2)").arg(m_partName).arg(languageName);
return m_partName;
}
private:
QString m_partName;
QStringList m_cSources, m_cxxSources, m_objcSources, m_objcxxSources;
int m_partCount;
};
} // anonymous namespace
ProjectPartBuilder::ProjectPartBuilder(ProjectInfo &pInfo)
: m_templatePart(new ProjectPart)
, m_pInfo(pInfo)
{
m_templatePart->project = pInfo.project();
m_templatePart->displayName = pInfo.project()->displayName();
m_templatePart->projectFile = pInfo.project()->projectFilePath().toString();
}
void ProjectPartBuilder::setQtVersion(ProjectPart::QtVersion qtVersion)
{
m_templatePart->qtVersion = qtVersion;
}
void ProjectPartBuilder::setCFlags(const QStringList &flags)
{
m_cFlags = flags;
}
void ProjectPartBuilder::setCxxFlags(const QStringList &flags)
{
m_cxxFlags = flags;
}
void ProjectPartBuilder::setDefines(const QByteArray &defines)
{
m_templatePart->projectDefines = defines;
}
void ProjectPartBuilder::setHeaderPaths(const ProjectPart::HeaderPaths &headerPaths)
{
m_templatePart->headerPaths = headerPaths;
}
void ProjectPartBuilder::setIncludePaths(const QStringList &includePaths)
{
m_templatePart->headerPaths.clear();
foreach (const QString &includeFile, includePaths) {
ProjectPart::HeaderPath hp(includeFile, ProjectPart::HeaderPath::IncludePath);
// The simple project managers are utterly ignorant of frameworks on OSX, and won't report
// framework paths. The work-around is to check if the include path ends in ".framework",
// and if so, add the parent directory as framework path.
if (includeFile.endsWith(QLatin1String(".framework"))) {
const int slashIdx = includeFile.lastIndexOf(QLatin1Char('/'));
if (slashIdx != -1) {
hp = ProjectPart::HeaderPath(includeFile.left(slashIdx),
ProjectPart::HeaderPath::FrameworkPath);
continue;
}
}
m_templatePart->headerPaths += hp;
}
}
void ProjectPartBuilder::setPreCompiledHeaders(const QStringList &pchs)
{
m_templatePart->precompiledHeaders = pchs;
}
void ProjectPartBuilder::setProjectFile(const QString &projectFile)
{
m_templatePart->projectFile = projectFile;
}
void ProjectPartBuilder::setDisplayName(const QString &displayName)
{
m_templatePart->displayName = displayName;
}
void ProjectPartBuilder::setConfigFileName(const QString &configFileName)
{
m_templatePart->projectConfigFile = configFileName;
}
QList<Core::Id> ProjectPartBuilder::createProjectPartsForFiles(const QStringList &files)
{
QList<Core::Id> languages;
ProjectFileCategorizer cat(m_templatePart->displayName, files);
if (cat.hasNoParts())
return languages;
using CppTools::ProjectFile;
using CppTools::ProjectPart;
if (cat.hasCSources()) {
createProjectPart(cat.cSources(),
cat.partName(QCoreApplication::translate("CppTools", "C11")),
ProjectPart::C11,
ProjectPart::CXX11);
// TODO: there is no C...
// languages += ProjectExplorer::Constants::LANG_C;
}
if (cat.hasObjcSources()) {
createProjectPart(cat.objcSources(),
cat.partName(QCoreApplication::translate("CppTools", "Obj-C11")),
ProjectPart::C11,
ProjectPart::CXX11);
// TODO: there is no Ojective-C...
// languages += ProjectExplorer::Constants::LANG_OBJC;
}
if (cat.hasCxxSources()) {
createProjectPart(cat.cxxSources(),
cat.partName(QCoreApplication::translate("CppTools", "C++11")),
ProjectPart::C11,
ProjectPart::CXX11);
languages += ProjectExplorer::Constants::LANG_CXX;
}
if (cat.hasObjcxxSources()) {
createProjectPart(cat.objcxxSources(),
cat.partName(QCoreApplication::translate("CppTools", "Obj-C++11")),
ProjectPart::C11,
ProjectPart::CXX11);
// TODO: there is no Objective-C++...
languages += ProjectExplorer::Constants::LANG_CXX;
}
return languages;
}
void ProjectPartBuilder::createProjectPart(const QStringList &theSources,
const QString &partName,
ProjectPart::CVersion cVersion,
ProjectPart::CXXVersion cxxVersion)
{
CppTools::ProjectPart::Ptr part(m_templatePart->copy());
part->displayName = partName;
Kit *k = part->project->activeTarget()->kit();
if (ToolChain *tc = ToolChainKitInformation::toolChain(k))
part->evaluateToolchain(tc, m_cFlags, m_cxxFlags, SysRootKitInformation::sysRoot(k));
part->cVersion = cVersion;
part->cxxVersion = cxxVersion;
CppTools::ProjectFileAdder adder(part->files);
foreach (const QString &file, theSources)
adder.maybeAdd(file);
m_pInfo.appendProjectPart(part);
}
...@@ -43,18 +43,7 @@ namespace CppTools { ...@@ -43,18 +43,7 @@ namespace CppTools {
class CPPTOOLS_EXPORT ProjectPart class CPPTOOLS_EXPORT ProjectPart
{ {
public: public: // Types
typedef QSharedPointer<ProjectPart> Ptr;
public:
ProjectPart();
void evaluateToolchain(const ProjectExplorer::ToolChain *tc,
const QStringList &cxxflags,
const QStringList &cflags,
const Utils::FileName &sysRoot);
public:
enum CVersion { enum CVersion {
C89, C89,
C99, C99,
...@@ -84,11 +73,12 @@ public: ...@@ -84,11 +73,12 @@ public:
Qt5 = 2 Qt5 = 2
}; };
class HeaderPath typedef QSharedPointer<ProjectPart> Ptr;
{
public: struct HeaderPath {
enum Type { InvalidPath, IncludePath, FrameworkPath }; enum Type { InvalidPath, IncludePath, FrameworkPath };
public:
QString path; QString path;
Type type; Type type;
...@@ -106,10 +96,19 @@ public: ...@@ -106,10 +96,19 @@ public:
}; };
typedef QList<HeaderPath> HeaderPaths; typedef QList<HeaderPath> HeaderPaths;
public: public: // methods
ProjectPart();
void evaluateToolchain(const ProjectExplorer::ToolChain *tc,
const QStringList &cxxflags,
const QStringList &cflags,
const Utils::FileName &sysRoot);
Ptr copy() const;
static QByteArray readProjectConfigFile(const ProjectPart::Ptr &part); static QByteArray readProjectConfigFile(const ProjectPart::Ptr &part);
public: public: // fields
QString displayName; QString displayName;
QString projectFile; QString projectFile;
ProjectExplorer::Project *project; ProjectExplorer::Project *project;
...@@ -159,6 +158,34 @@ private: ...@@ -159,6 +158,34 @@ private:
QByteArray m_defines; QByteArray m_defines;
}; };
class CPPTOOLS_EXPORT ProjectPartBuilder
{
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 ProjectPart::HeaderPaths &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);
QList<Core::Id> createProjectPartsForFiles(const QStringList &files);
private:
void createProjectPart(const QStringList &theSources, const QString &partName,
ProjectPart::CVersion cVersion, ProjectPart::CXXVersion cxxVersion);
private:
ProjectPart::Ptr m_templatePart;
ProjectInfo &m_pInfo;
QStringList m_cFlags, m_cxxFlags;
};
} // namespace CppTools } // namespace CppTools
#endif // CPPPROJECTPART_H #endif // CPPPROJECTPART_H
...@@ -266,47 +266,7 @@ void GenericProject::refresh(RefreshOptions options) ...@@ -266,47 +266,7 @@ void GenericProject::refresh(RefreshOptions options)
if (options & Files) if (options & Files)
m_rootNode->refresh(oldFileList); m_rootNode->refresh(oldFileList);
CppTools::CppModelManagerInterface *modelManager = refreshCppCodeModel();
CppTools::CppModelManagerInterface::instance();
if (modelManager) {
CppTools::ProjectInfo pinfo = modelManager->projectInfo(this);
pinfo.clearProjectParts();
CppTools::ProjectPart::Ptr part(new CppTools::ProjectPart);
part->project = this;
part->displayName = displayName();
part->projectFile = projectFilePath().toString();
foreach (const QString &inc, projectIncludePaths())
part->headerPaths += CppTools::ProjectPart::HeaderPath(
inc, CppTools::ProjectPart::HeaderPath::IncludePath);
Kit *k = activeTarget() ? activeTarget()->kit() : KitManager::defaultKit();
if (ToolChain *tc = ToolChainKitInformation::toolChain(k)) {
QStringList cflags;
QStringList cxxflags;
cxxflags << QLatin1String("-std=c++11");
part->evaluateToolchain(tc, cxxflags, cflags,
SysRootKitInformation::sysRoot(k));
}
part->projectConfigFile = configFileName();
// ### add _defines.
// Add any C/C++ files to be parsed
CppTools::ProjectFileAdder adder(part->files);
foreach (const QString &file, files())
adder.maybeAdd(file);
m_codeModelFuture.cancel();
pinfo.appendProjectPart(part);
setProjectLanguage(ProjectExplorer::Constants::LANG_CXX, !part->files.isEmpty());
m_codeModelFuture = modelManager->updateProjectInfo(pinfo);
}
} }
/** /**
...@@ -364,6 +324,31 @@ QStringList GenericProject::processEntries(const QStringList &paths, ...@@ -364,6 +324,31 @@ QStringList GenericProject::processEntries(const QStringList &paths,
return absolutePaths; return absolutePaths;
} }
void GenericProject::refreshCppCodeModel()
{
CppTools::CppModelManagerInterface *modelManager =
CppTools::CppModelManagerInterface::instance();
if (!modelManager)
return;
m_codeModelFuture.cancel();
CppTools::ProjectInfo pInfo = modelManager->projectInfo(this);
pInfo.clearProjectParts();
CppTools::ProjectPartBuilder ppBuilder(pInfo);
ppBuilder.setIncludePaths(projectIncludePaths());
ppBuilder.setConfigFileName(configFileName());
ppBuilder.setCxxFlags(QStringList() << QLatin1String("-std=c++11"));
const QList<Core::Id> languages = ppBuilder.createProjectPartsForFiles(files());
foreach (Core::Id language, languages)
setProjectLanguage(language, true);
m_codeModelFuture = modelManager->updateProjectInfo(pInfo);
}
QStringList GenericProject::projectIncludePaths() const QStringList GenericProject::projectIncludePaths() const
{ {
return m_projectIncludePaths; return m_projectIncludePaths;
......
...@@ -94,6 +94,8 @@ private: ...@@ -94,6 +94,8 @@ private:
QStringList processEntries(const QStringList &paths, QStringList processEntries(const QStringList &paths,
QHash<QString, QString> *map = 0) const; QHash<QString, QString> *map = 0) const;
void refreshCppCodeModel();
Manager *m_manager; Manager *m_manager;
QString m_fileName; QString m_fileName;
QString m_filesFileName; QString m_filesFileName;
......
...@@ -60,6 +60,8 @@ private slots: ...@@ -60,6 +60,8 @@ private slots:
#ifdef WITH_TESTS #ifdef WITH_TESTS
private slots: private slots:
void test_simple(); void test_simple();
void test_mixed1();
void test_mixed2();
#endif // WITH_TESTS #endif // WITH_TESTS
private: private:
......
...@@ -85,26 +85,124 @@ private: ...@@ -85,26 +85,124 @@ private:
}; };
} // anonymous namespace } // anonymous namespace
void GenericProjectPlugin::test_simple() static ProjectInfo setupProject(const QByteArray &projectFile, const QByteArray &mainFile,
ProjectExplorerHelper &pHelper)
{ {
CppModelManagerHelper cppHelper; CppModelManagerHelper cppHelper;
Project *project = pHelper.openProject(projectFilePath(_(projectFile)));
if (!project)
return ProjectInfo();
QString projectFile = _("testdata_simpleproject/simpleproject.creator"); // Wait only for a single file: we don't really care if the file is refreshed or not, but at
ProjectExplorerHelper pHelper; // this point we know that the C++ model manager got notified of all project parts and we can
Project *project = pHelper.openProject(projectFilePath(projectFile)); // retrieve them for inspection.
QVERIFY(project); cppHelper.waitForSourceFilesRefreshed(projectFilePath(_(mainFile)));
QString mainFile = projectFilePath(_("testdata_simpleproject/main.cpp"));
cppHelper.waitForSourceFilesRefreshed(mainFile);
CppModelManagerInterface *mm = cppHelper.cppModelManager(); CppModelManagerInterface *mm = cppHelper.cppModelManager();
ProjectInfo pInfo = mm->projectInfo(project); return mm->projectInfo(project);
}
void GenericProjectPlugin::test_simple()
{
ProjectExplorerHelper pHelper;
const QByteArray mainFile("testdata_simpleproject/main.cpp");
ProjectInfo pInfo(
setupProject("testdata_simpleproject/simpleproject.creator", mainFile, pHelper));
QVERIFY(pInfo);
QCOMPARE(pInfo.projectParts().size(), 1); QCOMPARE(pInfo.projectParts().size(), 1);
ProjectPart::Ptr pPart = pInfo.projectParts().first(); ProjectPart::Ptr pPart = pInfo.projectParts().first();
QVERIFY(pPart); QVERIFY(pPart);
QCOMPARE(pPart->files.size(), 1); QCOMPARE(pPart->files.size(), 1);
QCOMPARE(pPart->files.first().path, mainFile); QCOMPARE(pPart->files.first().path, projectFilePath(_(mainFile)));
QCOMPARE(pPart->files.first().kind, ProjectFile::CXXSource); QCOMPARE(pPart->files.first().kind, ProjectFile::CXXSource);
} }
static QStringList simplify(const QList<CppTools::ProjectFile> &files, const QString &prefix)
{
QStringList result;
foreach (const CppTools::ProjectFile &file, files) {