Commit b860d465 authored by Marco Bubke's avatar Marco Bubke

Clang: Handle generated files

We don't handled generated files so we got internal parse errors.

Change-Id: If75e202f93fe3f71f43e3b1d15c0fb77e20c2248
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent 55400d5b
......@@ -28,6 +28,8 @@
#include "clangbackendipc_global.h"
#include "filepath.h"
#include <vector>
namespace ClangBackEnd {
namespace V2 {
......@@ -104,6 +106,12 @@ public:
&& first.commandLineArguments_ == second.commandLineArguments_;
}
friend bool operator<(const FileContainer &first, const FileContainer &second)
{
return std::tie(first.documentRevision_, first.filePath_, first.unsavedFileContent_, first.commandLineArguments_)
< std::tie(second.documentRevision_, second.filePath_, second.unsavedFileContent_, second.commandLineArguments_);
}
FileContainer clone() const
{
return FileContainer(filePath_.clone(),
......@@ -119,9 +127,10 @@ private:
quint32 documentRevision_ = 0;
};
using FileContainers = std::vector<FileContainer>;
CMBIPC_EXPORT QDebug operator<<(QDebug debug, const FileContainer &container);
void PrintTo(const FileContainer &container, ::std::ostream* os);
} // namespace V2
} // namespace ClangBackEnd
......@@ -77,6 +77,11 @@ public:
return std::move(name_);
}
Utils::PathString path() const
{
return {directory_, "/", name_};
}
friend QDataStream &operator<<(QDataStream &out, const FilePath &filePath)
{
out << filePath.directory_;
......@@ -106,6 +111,12 @@ public:
&& first.directory_ == second.directory_;
}
friend bool operator<(const FilePath &first, const FilePath &second)
{
return std::tie(first.name_, first.directory_)
< std::tie(second.name_, second.directory_);
}
FilePath clone() const
{
return FilePath(directory_.clone(), name_.clone());
......
......@@ -38,8 +38,8 @@ public:
ProjectPartContainer() = default;
ProjectPartContainer(Utils::SmallString &&projectPartId,
Utils::SmallStringVector &&arguments,
Utils::SmallStringVector &&headerPaths,
Utils::SmallStringVector &&sourcePaths)
Utils::PathStringVector &&headerPaths,
Utils::PathStringVector &&sourcePaths)
: projectPartId_(std::move(projectPartId)),
arguments_(std::move(arguments)),
headerPaths_(std::move(headerPaths)),
......@@ -57,12 +57,12 @@ public:
return arguments_;
}
const Utils::SmallStringVector &sourcePaths() const
const Utils::PathStringVector &sourcePaths() const
{
return sourcePaths_;
}
const Utils::SmallStringVector &headerPaths() const
const Utils::PathStringVector &headerPaths() const
{
return headerPaths_;
}
......@@ -112,8 +112,8 @@ public:
private:
Utils::SmallString projectPartId_;
Utils::SmallStringVector arguments_;
Utils::SmallStringVector headerPaths_;
Utils::SmallStringVector sourcePaths_;
Utils::PathStringVector headerPaths_;
Utils::PathStringVector sourcePaths_;
};
using ProjectPartContainers = std::vector<ProjectPartContainer>;
......
......@@ -25,6 +25,7 @@
#pragma once
#include "filecontainerv2.h"
#include "projectpartcontainerv2.h"
namespace ClangBackEnd {
......@@ -33,8 +34,10 @@ class UpdatePchProjectPartsMessage
{
public:
UpdatePchProjectPartsMessage() = default;
UpdatePchProjectPartsMessage(V2::ProjectPartContainers &&projectsParts)
: projectsParts_(std::move(projectsParts))
UpdatePchProjectPartsMessage(V2::ProjectPartContainers &&projectsParts,
V2::FileContainers &&generatedFiles)
: projectsParts_(std::move(projectsParts)),
generatedFiles_(std::move(generatedFiles))
{}
const V2::ProjectPartContainers &projectsParts() const
......@@ -47,9 +50,20 @@ public:
return std::move(projectsParts_);
}
const V2::FileContainers &generatedFiles() const
{
return generatedFiles_;
}
V2::FileContainers takeGeneratedFiles()
{
return std::move(generatedFiles_);
}
friend QDataStream &operator<<(QDataStream &out, const UpdatePchProjectPartsMessage &message)
{
out << message.projectsParts_;
out << message.generatedFiles_;
return out;
}
......@@ -57,6 +71,7 @@ public:
friend QDataStream &operator>>(QDataStream &in, UpdatePchProjectPartsMessage &message)
{
in >> message.projectsParts_;
in >> message.generatedFiles_;
return in;
}
......@@ -64,16 +79,19 @@ public:
friend bool operator==(const UpdatePchProjectPartsMessage &first,
const UpdatePchProjectPartsMessage &second)
{
return first.projectsParts_ == second.projectsParts_;
return first.projectsParts_ == second.projectsParts_
&& first.generatedFiles_ == second.generatedFiles_;
}
UpdatePchProjectPartsMessage clone() const
{
return UpdatePchProjectPartsMessage(Utils::clone(projectsParts_));
return UpdatePchProjectPartsMessage(Utils::clone(projectsParts_),
Utils::clone(generatedFiles_));
}
private:
V2::ProjectPartContainers projectsParts_;
V2::FileContainers generatedFiles_;
};
CMBIPC_EXPORT QDebug operator<<(QDebug debug, const UpdatePchProjectPartsMessage &message);
......
......@@ -34,8 +34,24 @@
#include <cpptools/clangcompileroptionsbuilder.h>
#include <cpptools/projectpart.h>
#include <algorithm>
#include <functional>
namespace ClangPchManager {
class HeaderAndSources
{
public:
void reserve(std::size_t size)
{
headers.reserve(size);
sources.reserve(size);
}
Utils::PathStringVector headers;
Utils::PathStringVector sources;
};
ProjectUpdater::ProjectUpdater(ClangBackEnd::PchManagerServerInterface &server,
PchManagerClient &client)
: m_server(server),
......@@ -43,9 +59,13 @@ ProjectUpdater::ProjectUpdater(ClangBackEnd::PchManagerServerInterface &server,
{
}
void ProjectUpdater::updateProjectParts(const std::vector<CppTools::ProjectPart *> &projectParts)
void ProjectUpdater::updateProjectParts(const std::vector<CppTools::ProjectPart *> &projectParts,
ClangBackEnd::V2::FileContainers &&generatedFiles)
{
ClangBackEnd::UpdatePchProjectPartsMessage message{toProjectPartContainers(projectParts)};
m_excludedPaths = createExcludedPaths(generatedFiles);
ClangBackEnd::UpdatePchProjectPartsMessage message{toProjectPartContainers(projectParts),
std::move(generatedFiles)};
m_server.updatePchProjectParts(std::move(message));
}
......@@ -60,37 +80,39 @@ void ProjectUpdater::removeProjectParts(const QStringList &projectPartIds)
m_client.precompiledHeaderRemoved(projectPartiId);
}
namespace {
class HeaderAndSources
void ProjectUpdater::setExcludedPaths(Utils::PathStringVector &&excludedPaths)
{
public:
void reserve(std::size_t size)
{
headers.reserve(size);
sources.reserve(size);
}
Utils::SmallStringVector headers;
Utils::SmallStringVector sources;
};
m_excludedPaths = excludedPaths;
}
HeaderAndSources headerAndSourcesFromProjectPart(CppTools::ProjectPart *projectPart)
void ProjectUpdater::addToHeaderAndSources(HeaderAndSources &headerAndSources,
const CppTools::ProjectFile &projectFile) const
{
HeaderAndSources headerAndSources;
headerAndSources.reserve(std::size_t(projectPart->files.size()) * 3 / 2);
Utils::PathString path = projectFile.path;
bool exclude = std::binary_search(m_excludedPaths.begin(), m_excludedPaths.end(), path);
for (const CppTools::ProjectFile &projectFile : projectPart->files) {
if (!exclude) {
if (projectFile.isSource())
headerAndSources.sources.push_back(projectFile.path);
headerAndSources.sources.push_back(path);
else if (projectFile.isHeader())
headerAndSources.headers.push_back(projectFile.path);
headerAndSources.headers.push_back(path);
}
}
HeaderAndSources ProjectUpdater::headerAndSourcesFromProjectPart(
CppTools::ProjectPart *projectPart) const
{
HeaderAndSources headerAndSources;
headerAndSources.reserve(std::size_t(projectPart->files.size()) * 3 / 2);
for (const CppTools::ProjectFile &projectFile : projectPart->files)
addToHeaderAndSources(headerAndSources, projectFile);
return headerAndSources;
}
}
ClangBackEnd::V2::ProjectPartContainer ProjectUpdater::toProjectPartContainer(CppTools::ProjectPart *projectPart)
ClangBackEnd::V2::ProjectPartContainer ProjectUpdater::toProjectPartContainer(
CppTools::ProjectPart *projectPart) const
{
using CppTools::ClangCompilerOptionsBuilder;
......@@ -110,17 +132,39 @@ ClangBackEnd::V2::ProjectPartContainer ProjectUpdater::toProjectPartContainer(Cp
}
std::vector<ClangBackEnd::V2::ProjectPartContainer> ProjectUpdater::toProjectPartContainers(
std::vector<CppTools::ProjectPart *> projectParts)
std::vector<CppTools::ProjectPart *> projectParts) const
{
using namespace std::placeholders;
std::vector<ClangBackEnd::V2::ProjectPartContainer> projectPartContainers;
projectPartContainers.reserve(projectParts.size());
std::transform(projectParts.begin(),
projectParts.end(),
std::back_inserter(projectPartContainers),
ProjectUpdater::toProjectPartContainer);
std::bind(&ProjectUpdater::toProjectPartContainer, this, _1));
return projectPartContainers;
}
Utils::PathStringVector ProjectUpdater::createExcludedPaths(
const ClangBackEnd::V2::FileContainers &generatedFiles)
{
Utils::PathStringVector excludedPaths;
excludedPaths.reserve(generatedFiles.size());
auto convertToPath = [] (const ClangBackEnd::V2::FileContainer &fileContainer) {
return fileContainer.filePath().path();
};
std::transform(generatedFiles.begin(),
generatedFiles.end(),
std::back_inserter(excludedPaths),
convertToPath);
std::sort(excludedPaths.begin(), excludedPaths.end());
return excludedPaths;
}
} // namespace ClangPchManager
......@@ -27,8 +27,11 @@
#include <clangpchmanager_global.h>
#include <filecontainerv2.h>
namespace CppTools {
class ProjectPart;
class ProjectFile;
}
namespace ClangBackEnd {
......@@ -45,6 +48,7 @@ QT_FORWARD_DECLARE_CLASS(QStringList)
namespace ClangPchManager {
class HeaderAndSources;
class PchManagerClient;
class ProjectUpdater
......@@ -53,16 +57,26 @@ public:
ProjectUpdater(ClangBackEnd::PchManagerServerInterface &server,
PchManagerClient &client);
void updateProjectParts(const std::vector<CppTools::ProjectPart *> &projectParts);
void updateProjectParts(const std::vector<CppTools::ProjectPart *> &projectParts,
ClangBackEnd::V2::FileContainers &&generatedFiles);
void removeProjectParts(const QStringList &projectPartIds);
unittest_public:
static ClangBackEnd::V2::ProjectPartContainer toProjectPartContainer(
CppTools::ProjectPart *projectPart);
static std::vector<ClangBackEnd::V2::ProjectPartContainer> toProjectPartContainers(
std::vector<CppTools::ProjectPart *> projectParts);
void setExcludedPaths(Utils::PathStringVector &&excludedPaths);
HeaderAndSources headerAndSourcesFromProjectPart(CppTools::ProjectPart *projectPart) const;
ClangBackEnd::V2::ProjectPartContainer toProjectPartContainer(
CppTools::ProjectPart *projectPart) const;
std::vector<ClangBackEnd::V2::ProjectPartContainer> toProjectPartContainers(
std::vector<CppTools::ProjectPart *> projectParts) const;
void addToHeaderAndSources(HeaderAndSources &headerAndSources,
const CppTools::ProjectFile &projectFile) const;
static Utils::PathStringVector createExcludedPaths(
const ClangBackEnd::V2::FileContainers &generatedFiles);
private:
Utils::PathStringVector m_excludedPaths;
ClangBackEnd::PchManagerServerInterface &m_server;
PchManagerClient &m_client;
};
......
......@@ -25,6 +25,7 @@
#include "qtcreatorprojectupdater.h"
#include <cpptools/abstracteditorsupport.h>
#include <cpptools/cppmodelmanager.h>
#include <projectexplorer/project.h>
......@@ -43,7 +44,29 @@ QtCreatorProjectUpdater::QtCreatorProjectUpdater(ClangBackEnd::PchManagerServerI
connectToCppModelManager();
}
void QtCreatorProjectUpdater::projectPartsUpdated(ProjectExplorer::Project *project)
namespace {
std::vector<ClangBackEnd::V2::FileContainer> createGeneratedFiles()
{
auto abstractEditors = CppTools::CppModelManager::instance()->abstractEditorSupports();
std::vector<ClangBackEnd::V2::FileContainer> generatedFiles;
generatedFiles.reserve(std::size_t(abstractEditors.size()));
auto toFileContainer = [] (const CppTools::AbstractEditorSupport *abstractEditor) {
return ClangBackEnd::V2::FileContainer(ClangBackEnd::FilePath(abstractEditor->fileName()),
Utils::SmallString::fromQByteArray(abstractEditor->contents()),
{});
};
std::transform(abstractEditors.begin(),
abstractEditors.end(),
std::back_inserter(generatedFiles),
toFileContainer);
return generatedFiles;
}
std::vector<CppTools::ProjectPart*> createProjectParts(ProjectExplorer::Project *project)
{
const CppTools::ProjectInfo projectInfo = cppModelManager()->projectInfo(project);
......@@ -61,7 +84,14 @@ void QtCreatorProjectUpdater::projectPartsUpdated(ProjectExplorer::Project *proj
std::back_inserter(projectParts),
convertToRawPointer);
updateProjectParts(projectParts);
return projectParts;
}
}
void QtCreatorProjectUpdater::projectPartsUpdated(ProjectExplorer::Project *project)
{
updateProjectParts(createProjectParts(project), createGeneratedFiles());
}
void QtCreatorProjectUpdater::projectPartsRemoved(const QStringList &projectPartIds)
......
......@@ -38,12 +38,26 @@ class CollectIncludesToolAction final : public clang::tooling::FrontendActionFac
public:
CollectIncludesToolAction(std::vector<uint> &includeIds,
StringCache<Utils::SmallString> &filePathCache,
const std::vector<uint> &excludedIncludeUIDs)
const Utils::PathStringVector &excludedIncludes)
: m_includeIds(includeIds),
m_filePathCache(filePathCache),
m_excludedIncludeUIDs(excludedIncludeUIDs)
m_excludedIncludes(excludedIncludes)
{}
bool runInvocation(clang::CompilerInvocation *invocation,
clang::FileManager *fileManager,
std::shared_ptr<clang::PCHContainerOperations> pchContainerOperations,
clang::DiagnosticConsumer *diagnosticConsumer) override
{
if (m_excludedIncludeUIDs.empty())
m_excludedIncludeUIDs = generateExcludedIncludeFileUIDs(*fileManager);
return clang::tooling::FrontendActionFactory::runInvocation(invocation,
fileManager,
pchContainerOperations,
diagnosticConsumer);
}
clang::FrontendAction *create()
{
return new CollectIncludesAction(m_includeIds,
......@@ -52,11 +66,29 @@ public:
m_alreadyIncludedFileUIDs);
}
std::vector<uint> generateExcludedIncludeFileUIDs(clang::FileManager &fileManager) const
{
std::vector<uint> fileUIDs;
fileUIDs.reserve(m_excludedIncludes.size());
for (const Utils::PathString &filePath : m_excludedIncludes) {
const clang::FileEntry *file = fileManager.getFile({filePath.data(), filePath.size()});
if (file)
fileUIDs.push_back(file->getUID());
}
std::sort(fileUIDs.begin(), fileUIDs.end());
return fileUIDs;
}
private:
std::vector<uint> m_alreadyIncludedFileUIDs;
std::vector<uint> m_excludedIncludeUIDs;
std::vector<uint> &m_includeIds;
StringCache<Utils::SmallString> &m_filePathCache;
const std::vector<uint> &m_excludedIncludeUIDs;
const Utils::PathStringVector &m_excludedIncludes;
};
} // namespace ClangBackEnd
......@@ -29,6 +29,8 @@
#include <utils/smallstring.h>
#include <algorithm>
namespace ClangBackEnd {
IncludeCollector::IncludeCollector(StringCache<Utils::SmallString> &filePathCache)
......@@ -40,17 +42,31 @@ void IncludeCollector::collectIncludes()
{
clang::tooling::ClangTool tool = createTool();
auto excludedIncludeFileUIDs = generateExcludedIncludeFileUIDs(tool.getFiles());
auto action = std::unique_ptr<CollectIncludesToolAction>(
new CollectIncludesToolAction(m_includeIds, m_filePathCache, excludedIncludeFileUIDs));
new CollectIncludesToolAction(m_includeIds,
m_filePathCache,
m_excludedIncludes));
tool.run(action.get());
}
void IncludeCollector::setExcludedIncludes(Utils::SmallStringVector &&excludedIncludes)
void IncludeCollector::setExcludedIncludes(Utils::PathStringVector &&excludedIncludes)
{
this->m_excludedIncludes = std::move(excludedIncludes);
#ifdef _WIN32
m_excludedIncludes.clear();
m_excludedIncludes.reserve(excludedIncludes.size());
std::transform(std::make_move_iterator(excludedIncludes.begin()),
std::make_move_iterator(excludedIncludes.end()),
std::back_inserter(m_excludedIncludes),
[] (Utils::PathString &&path) {
path.replace("/", "\\");
return std::move(path);
});
#else
m_excludedIncludes = std::move(excludedIncludes);
#endif
}
std::vector<uint> IncludeCollector::takeIncludeIds()
......@@ -60,21 +76,6 @@ std::vector<uint> IncludeCollector::takeIncludeIds()
return std::move(m_includeIds);
}
std::vector<uint> IncludeCollector::generateExcludedIncludeFileUIDs(clang::FileManager &fileManager) const
{
std::vector<uint> fileUIDs;
fileUIDs.reserve(m_excludedIncludes.size());
for (const Utils::SmallString &filePath : m_excludedIncludes) {
const clang::FileEntry *file = fileManager.getFile({filePath.data(), filePath.size()});
if (file)
fileUIDs.push_back(file->getUID());
}
std::sort(fileUIDs.begin(), fileUIDs.end());
return fileUIDs;
}
} // namespace ClangBackEnd
......@@ -38,15 +38,12 @@ public:
void collectIncludes();
void setExcludedIncludes(Utils::SmallStringVector &&excludedIncludes);
void setExcludedIncludes(Utils::PathStringVector &&excludedIncludes);
std::vector<uint> takeIncludeIds();
private:
std::vector<uint> generateExcludedIncludeFileUIDs(clang::FileManager &fileManager) const;
private:
Utils::SmallStringVector m_excludedIncludes;
Utils::PathStringVector m_excludedIncludes;
std::vector<uint> m_includeIds;
Utils::SmallStringVector m_directories;
StringCache<Utils::SmallString> &m_filePathCache;
......
......@@ -45,14 +45,21 @@ PchCreator::PchCreator(Environment &environment, StringCache<Utils::SmallString>
PchCreator::PchCreator(V2::ProjectPartContainers &&projectsParts,
Environment &environment,
StringCache<Utils::SmallString> &filePathCache,
PchGeneratorInterface *pchGenerator)
PchGeneratorInterface *pchGenerator,
V2::FileContainers &&generatedFiles)
: m_projectParts(std::move(projectsParts)),
m_generatedFiles(std::move(generatedFiles)),
m_environment(environment),
m_filePathCache(filePathCache),
m_pchGenerator(pchGenerator)
{
}
void PchCreator::setGeneratedFiles(V2::FileContainers &&generatedFiles)
{
m_generatedFiles = generatedFiles;
}
namespace {
template <typename Source,
typename Target>
......@@ -92,12 +99,13 @@ std::size_t globalCount(const V2::ProjectPartContainers &projectsParts,
sizeFunction);
}
template <typename GetterFunction>
void generateGlobal(Utils::SmallStringVector &entries,
template <typename Container,
typename GetterFunction>
void generateGlobal(Container &entries,
const V2::ProjectPartContainers &projectsParts,
GetterFunction getterFunction)
{
entries.reserve(entries.size() + globalCount(projectsParts, getterFunction));
entries.reserve(entries.capacity() + globalCount(projectsParts, getterFunction));
for (const V2::ProjectPartContainer &projectPart : projectsParts) {
const auto &projectPartPaths = getterFunction(projectPart);
......@@ -106,41 +114,66 @@ void generateGlobal(Utils::SmallStringVector &entries,
};
}
template <typename GetterFunction>
Utils::SmallStringVector generateGlobal(
template <typename Container,
typename GetterFunction>
Utils::PathStringVector generateGlobal(
const V2::ProjectPartContainers &projectsParts,
GetterFunction getterFunction)
GetterFunction getterFunction,
std::size_t prereserve = 0)
{
Utils::SmallStringVector entries;
Container entries;
entries.reserve(prereserve);
generateGlobal(entries, projectsParts, getterFunction);
return entries;
}
Utils::PathStringVector generatedFilePaths(const V2::FileContainers &generaredFiles)
{
Utils::PathStringVector generaredFilePaths;
generaredFilePaths.reserve(generaredFiles.size());
for (const V2::FileContainer &generatedFile : generaredFiles)
generaredFilePaths.push_back(generatedFile.filePath().path());
return generaredFilePaths;
}
}
Utils::SmallStringVector PchCreator::generateGlobalHeaderPaths() const
Utils::PathStringVector PchCreator::generateGlobalHeaderPaths() const
{
auto includeFunction = [] (const V2::ProjectPartContainer &projectPart)
-> const Utils::SmallStringVector & {
-> const Utils::PathStringVector & {
return projectPart.headerPaths();
};
return generateGlobal(m_projectParts, includeFunction);
Utils::PathStringVector headerPaths = generateGlobal<Utils::PathStringVector>(m_projectParts,
includeFunction,
m_generatedFiles.size());
Utils::PathStringVector generatedPath = generatedFilePaths(m_generatedFiles);
headerPaths.insert(headerPaths.end(),
std::make_move_iterator(generatedPath.begin()),