Commit 38f72855 authored by Nikolai Kosjar's avatar Nikolai Kosjar

Clang: Process distinct documents concurrently

Speed ups the typical use cases that can profit from this:
 * Change a header file and switch then to source file
 * Open documents one after the other (Follow Symbol)
 * Change documents visible in splits (e.g. by refactoring action)
 * Restore a session with multiple splits

Fixes the test ClangIpcServer.GetCodeCompletionDependingOnArgumets.

Change-Id: Ia575bd59780df14146dfc091a4d48794e4a0543d
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent 90f94363
/****************************************************************************
**
** 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 "clangiasyncjob.h"
#include <utils/runextensions.h>
#include <QFutureWatcher>
#include <QObject>
namespace ClangBackEnd {
template<class Result>
class AsyncJob : public IAsyncJob
{
public:
AsyncJob() {}
~AsyncJob() {}
using Runner = std::function<Result()>;
Runner runner() const { return m_runner; }
void setRunner(const Runner &runner) { m_runner = runner; }
Result asyncResult() const { return m_futureWatcher.future().result(); }
QFuture<void> runAsync() override
{
const auto onFinished = [this]() {
finalizeAsyncRun();
setIsFinished(true);
finishedHandler()(this);
};
QObject::connect(&m_futureWatcher,
&QFutureWatcher<Result>::finished,
onFinished);
const QFuture<Result> future = Utils::runAsync(m_runner);
m_futureWatcher.setFuture(future);
return future;
}
private:
Runner m_runner;
QFutureWatcher<Result> m_futureWatcher;
};
} // namespace ClangBackEnd
......@@ -36,9 +36,19 @@ HEADERS += $$PWD/clangcodemodelserver.h \
$$PWD/highlightingmarks.h \
$$PWD/highlightingmarksiterator.h \
$$PWD/utf8positionfromlinecolumn.h \
$$PWD/clangasyncjob.h \
$$PWD/clangcompletecodejob.h \
$$PWD/clangcreateinitialdocumentpreamblejob.h \
$$PWD/clangfilepath.h \
$$PWD/clangiasyncjob.h \
$$PWD/clangjobcontext.h \
$$PWD/clangjobqueue.h \
$$PWD/clangjobrequest.h \
$$PWD/clangjobs.h \
$$PWD/clangrequestdocumentannotationsjob.h \
$$PWD/clangtranslationunitcore.h \
$$PWD/clangunsavedfilesshallowarguments.h \
$$PWD/clangtranslationunitcore.h
$$PWD/clangupdatedocumentannotationsjob.h
SOURCES += $$PWD/clangcodemodelserver.cpp \
$$PWD/codecompleter.cpp \
......@@ -74,6 +84,16 @@ SOURCES += $$PWD/clangcodemodelserver.cpp \
$$PWD/highlightingmark.cpp \
$$PWD/highlightingmarks.cpp \
$$PWD/utf8positionfromlinecolumn.cpp \
$$PWD/clangcompletecodejob.cpp \
$$PWD/clangcreateinitialdocumentpreamblejob.cpp \
$$PWD/clangfilepath.cpp \
$$PWD/clangiasyncjob.cpp \
$$PWD/clangjobcontext.cpp \
$$PWD/clangjobqueue.cpp \
$$PWD/clangjobrequest.cpp \
$$PWD/clangjobs.cpp \
$$PWD/clangrequestdocumentannotationsjob.cpp \
$$PWD/clangtranslationunitcore.cpp \
$$PWD/clangunsavedfilesshallowarguments.cpp \
$$PWD/clangtranslationunitcore.cpp
$$PWD/clangupdatedocumentannotationsjob.cpp \
......@@ -82,41 +82,25 @@ int delayedDocumentAnnotationsTimerInterval()
return interval;
}
}
} // anonymous
ClangCodeModelServer::ClangCodeModelServer()
: translationUnits(projects, unsavedFiles)
, updateDocumentAnnotationsTimeOutInMs(delayedDocumentAnnotationsTimerInterval())
{
const auto sendDocumentAnnotations
= [this] (const DocumentAnnotationsChangedMessage &documentAnnotationsChangedMessage) {
client()->documentAnnotationsChanged(documentAnnotationsChangedMessage);
};
const auto sendDelayedDocumentAnnotations = [this] () {
try {
auto sendState = translationUnits.sendDocumentAnnotations();
if (sendState == DocumentAnnotationsSendState::MaybeThereAreDocumentAnnotations)
sendDocumentAnnotationsTimer.setInterval(0);
else
sendDocumentAnnotationsTimer.stop();
} catch (const std::exception &exception) {
qWarning() << "Error in ClangCodeModelServer::sendDelayedDocumentAnnotationsTimer:" << exception.what();
}
};
const auto onFileChanged = [this] (const Utf8String &filePath) {
startDocumentAnnotationsTimerIfFileIsNotATranslationUnit(filePath);
};
translationUnits.setSendDocumentAnnotationsCallback(sendDocumentAnnotations);
updateDocumentAnnotationsTimer.setSingleShot(true);
QObject::connect(&sendDocumentAnnotationsTimer,
QObject::connect(&updateDocumentAnnotationsTimer,
&QTimer::timeout,
sendDelayedDocumentAnnotations);
[this]() {
processJobsForDirtyAndVisibleDocuments();
});
QObject::connect(translationUnits.clangFileSystemWatcher(),
&ClangFileSystemWatcher::fileChanged,
onFileChanged);
[this](const Utf8String &filePath) {
ClangCodeModelServer::startDocumentAnnotationsTimerIfFileIsNotATranslationUnit(filePath);
});
}
void ClangCodeModelServer::end()
......@@ -133,8 +117,8 @@ void ClangCodeModelServer::registerTranslationUnitsForEditor(const ClangBackEnd:
unsavedFiles.createOrUpdate(message.fileContainers());
translationUnits.setUsedByCurrentEditor(message.currentEditorFilePath());
translationUnits.setVisibleInEditors(message.visibleEditorFilePaths());
startDocumentAnnotations();
reparseVisibleDocuments(createdTranslationUnits);
processInitialJobsForDocuments(createdTranslationUnits);
} catch (const ProjectPartDoNotExistException &exception) {
client()->projectPartsDoNotExist(ProjectPartsDoNotExistMessage(exception.projectPartIds()));
} catch (const std::exception &exception) {
......@@ -151,7 +135,8 @@ void ClangCodeModelServer::updateTranslationUnitsForEditor(const UpdateTranslati
if (newerFileContainers.size() > 0) {
translationUnits.update(newerFileContainers);
unsavedFiles.createOrUpdate(newerFileContainers);
sendDocumentAnnotationsTimer.start(delayedDocumentAnnotationsTimerInterval());
updateDocumentAnnotationsTimer.start(updateDocumentAnnotationsTimeOutInMs);
}
} catch (const ProjectPartDoNotExistException &exception) {
client()->projectPartsDoNotExist(ProjectPartsDoNotExistMessage(exception.projectPartIds()));
......@@ -185,7 +170,8 @@ void ClangCodeModelServer::registerProjectPartsForEditor(const RegisterProjectPa
try {
projects.createOrUpdate(message.projectContainers());
translationUnits.setTranslationUnitsDirtyIfProjectPartChanged();
sendDocumentAnnotationsTimer.start(0);
processJobsForDirtyAndVisibleDocuments();
} catch (const std::exception &exception) {
qWarning() << "Error in ClangCodeModelServer::registerProjectPartsForEditor:" << exception.what();
}
......@@ -211,7 +197,8 @@ void ClangCodeModelServer::registerUnsavedFilesForEditor(const RegisterUnsavedFi
try {
unsavedFiles.createOrUpdate(message.fileContainers());
translationUnits.updateTranslationUnitsWithChangedDependencies(message.fileContainers());
sendDocumentAnnotationsTimer.start(delayedDocumentAnnotationsTimerInterval());
updateDocumentAnnotationsTimer.start(updateDocumentAnnotationsTimeOutInMs);
} catch (const ProjectPartDoNotExistException &exception) {
client()->projectPartsDoNotExist(ProjectPartsDoNotExistMessage(exception.projectPartIds()));
} catch (const std::exception &exception) {
......@@ -240,16 +227,16 @@ void ClangCodeModelServer::completeCode(const ClangBackEnd::CompleteCodeMessage
TIME_SCOPE_DURATION("ClangCodeModelServer::completeCode");
try {
auto translationUnit = translationUnits.translationUnit(message.filePath(), message.projectPartId());
auto translationUnitCore = translationUnit.translationUnitCore();
auto translationUnit = translationUnits.translationUnit(message.filePath(),
message.projectPartId());
CodeCompleter codeCompleter(translationUnitCore, unsavedFiles);
JobRequest jobRequest = createJobRequest(translationUnit, JobRequest::Type::CompleteCode);
jobRequest.line = message.line();
jobRequest.column = message.column();
jobRequest.ticketNumber = message.ticketNumber();
const auto codeCompletions = codeCompleter.complete(message.line(), message.column());
client()->codeCompleted(CodeCompletedMessage(codeCompletions,
codeCompleter.neededCorrection(),
message.ticketNumber()));
jobs().add(jobRequest);
jobs().process();
} catch (const TranslationUnitDoesNotExistException &exception) {
client()->translationUnitDoesNotExist(TranslationUnitDoesNotExistMessage(exception.fileContainer()));
} catch (const ProjectPartDoNotExistException &exception) {
......@@ -266,13 +253,12 @@ void ClangCodeModelServer::requestDocumentAnnotations(const RequestDocumentAnnot
try {
auto translationUnit = translationUnits.translationUnit(message.fileContainer().filePath(),
message.fileContainer().projectPartId());
auto translationUnitCore = translationUnit.translationUnitCore();
client()->documentAnnotationsChanged(DocumentAnnotationsChangedMessage(
translationUnit.fileContainer(),
translationUnitCore.mainFileDiagnostics(),
translationUnitCore.highlightingMarks().toHighlightingMarksContainers(),
translationUnitCore.skippedSourceRanges().toSourceRangeContainers()));
const JobRequest jobRequest = createJobRequest(translationUnit,
JobRequest::Type::RequestDocumentAnnotations);
jobs().add(jobRequest);
jobs().process();
} catch (const TranslationUnitDoesNotExistException &exception) {
client()->translationUnitDoesNotExist(TranslationUnitDoesNotExistMessage(exception.fileContainer()));
} catch (const ProjectPartDoNotExistException &exception) {
......@@ -289,7 +275,7 @@ void ClangCodeModelServer::updateVisibleTranslationUnits(const UpdateVisibleTran
try {
translationUnits.setUsedByCurrentEditor(message.currentEditorFilePath());
translationUnits.setVisibleInEditors(message.visibleEditorFilePaths());
sendDocumentAnnotationsTimer.start(0);
updateDocumentAnnotationsTimer.start(0);
} catch (const std::exception &exception) {
qWarning() << "Error in ClangCodeModelServer::updateVisibleTranslationUnits:" << exception.what();
}
......@@ -302,23 +288,80 @@ const TranslationUnits &ClangCodeModelServer::translationUnitsForTestOnly() cons
void ClangCodeModelServer::startDocumentAnnotationsTimerIfFileIsNotATranslationUnit(const Utf8String &filePath)
{
if (!translationUnits.hasTranslationUnit(filePath))
sendDocumentAnnotationsTimer.start(0);
if (!translationUnits.hasTranslationUnitWithFilePath(filePath))
updateDocumentAnnotationsTimer.start(0);
}
const Jobs &ClangCodeModelServer::jobsForTestOnly()
{
return jobs();
}
bool ClangCodeModelServer::isTimerRunningForTestOnly() const
{
return updateDocumentAnnotationsTimer.isActive();
}
void ClangCodeModelServer::startDocumentAnnotations()
void ClangCodeModelServer::addJobRequestsForDirtyAndVisibleDocuments()
{
DocumentAnnotationsSendState sendState = DocumentAnnotationsSendState::MaybeThereAreDocumentAnnotations;
for (const auto &translationUnit : translationUnits.translationUnits()) {
if (translationUnit.isNeedingReparse() && translationUnit.isVisibleInEditor()) {
jobs().add(createJobRequest(translationUnit,
JobRequest::Type::UpdateDocumentAnnotations));
}
}
}
void ClangCodeModelServer::processJobsForDirtyAndVisibleDocuments()
{
addJobRequestsForDirtyAndVisibleDocuments();
jobs().process();
}
void ClangCodeModelServer::processInitialJobsForDocuments(
const std::vector<TranslationUnit> &translationUnits)
{
for (const auto &translationUnit : translationUnits) {
jobs().add(createJobRequest(translationUnit,
JobRequest::Type::UpdateDocumentAnnotations));
jobs().add(createJobRequest(translationUnit,
JobRequest::Type::CreateInitialDocumentPreamble));
}
jobs().process();
}
while (sendState == DocumentAnnotationsSendState::MaybeThereAreDocumentAnnotations)
sendState = translationUnits.sendDocumentAnnotations();
JobRequest ClangCodeModelServer::createJobRequest(const TranslationUnit &translationUnit,
JobRequest::Type type) const
{
JobRequest jobRequest;
jobRequest.type = type;
jobRequest.requirements = JobRequest::requirementsForType(type);
jobRequest.filePath = translationUnit.filePath();
jobRequest.projectPartId = translationUnit.projectPartId();
jobRequest.unsavedFilesChangeTimePoint = unsavedFiles.lastChangeTimePoint();
jobRequest.documentRevision = translationUnit.documentRevision();
const ProjectPart &projectPart = projects.project(translationUnit.projectPartId());
jobRequest.projectChangeTimePoint = projectPart.lastChangeTimePoint();
return jobRequest;
}
void ClangCodeModelServer::reparseVisibleDocuments(std::vector<TranslationUnit> &translationUnits)
void ClangCodeModelServer::setUpdateDocumentAnnotationsTimeOutInMsForTestsOnly(int value)
{
for (TranslationUnit &translationUnit : translationUnits)
if (translationUnit.isVisibleInEditor())
translationUnit.reparse();
updateDocumentAnnotationsTimeOutInMs = value;
}
Jobs &ClangCodeModelServer::jobs()
{
if (!jobs_) {
// Jobs needs a reference to the client, but the client is not known at
// construction time of ClangCodeModelServer, so construct Jobs in a
// lazy manner.
jobs_.reset(new Jobs(translationUnits, unsavedFiles, projects, *client()));
}
return *jobs_.data();
}
} // namespace ClangBackEnd
......@@ -32,10 +32,11 @@
#include "clangtranslationunit.h"
#include "translationunits.h"
#include "unsavedfiles.h"
#include "clangjobs.h"
#include <utf8string.h>
#include <QMap>
#include <QScopedPointer>
#include <QTimer>
namespace ClangBackEnd {
......@@ -57,18 +58,31 @@ public:
void updateVisibleTranslationUnits(const UpdateVisibleTranslationUnitsMessage &message) override;
void requestDocumentAnnotations(const RequestDocumentAnnotationsMessage &message) override;
public /*for tests*/:
const TranslationUnits &translationUnitsForTestOnly() const;
const Jobs &jobsForTestOnly();
bool isTimerRunningForTestOnly() const;
void setUpdateDocumentAnnotationsTimeOutInMsForTestsOnly(int value);
private:
Jobs &jobs();
void startDocumentAnnotationsTimerIfFileIsNotATranslationUnit(const Utf8String &filePath);
void startDocumentAnnotations();
void reparseVisibleDocuments(std::vector<TranslationUnit> &translationUnits);
void addJobRequestsForDirtyAndVisibleDocuments();
void processJobsForDirtyAndVisibleDocuments();
void processInitialJobsForDocuments(const std::vector<TranslationUnit> &translationUnits);
JobRequest createJobRequest(const TranslationUnit &translationUnit,
JobRequest::Type type) const;
private:
ProjectParts projects;
UnsavedFiles unsavedFiles;
TranslationUnits translationUnits;
QTimer sendDocumentAnnotationsTimer;
QScopedPointer<Jobs> jobs_;
QTimer updateDocumentAnnotationsTimer;
int updateDocumentAnnotationsTimeOutInMs;
};
} // namespace ClangBackEnd
/****************************************************************************
**
** 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 "clangcompletecodejob.h"
#include <clangbackendipc/clangbackendipcdebugutils.h>
#include <clangbackendipc/clangcodemodelclientinterface.h>
#include <clangbackendipc/cmbcodecompletedmessage.h>
#include <utils/qtcassert.h>
namespace ClangBackEnd {
static CompleteCodeJob::AsyncResult runAsyncHelper(const TranslationUnitCore &translationUnitCore,
UnsavedFiles unsavedFiles,
quint32 line,
quint32 column)
{
TIME_SCOPE_DURATION("CompleteCodeJobRunner");
CompleteCodeJob::AsyncResult asyncResult;
try {
const TranslationUnitCore::CodeCompletionResult results
= translationUnitCore.complete(unsavedFiles, line, column);
asyncResult.completions = results.completions;
asyncResult.correction = results.correction;
} catch (const std::exception &exception) {
qWarning() << "Error in CompleteCodeJobRunner:" << exception.what();
}
return asyncResult;
}
bool CompleteCodeJob::prepareAsyncRun()
{
const JobRequest jobRequest = context().jobRequest;
QTC_ASSERT(jobRequest.type == JobRequest::Type::CompleteCode, return false);
try {
m_pinnedTranslationUnit = context().translationUnitForJobRequest();
const TranslationUnitCore translationUnitCore = m_pinnedTranslationUnit.translationUnitCore();
const UnsavedFiles unsavedFiles = *context().unsavedFiles;
const quint32 line = jobRequest.line;
const quint32 column = jobRequest.column;
setRunner([translationUnitCore, unsavedFiles, line, column]() {
return runAsyncHelper(translationUnitCore, unsavedFiles, line, column);
});
} catch (const std::exception &exception) {
qWarning() << "Error in CompleteCodeJob::prepareAsyncRun:" << exception.what();
return false;
}
return true;
}
void CompleteCodeJob::finalizeAsyncRun()
{
if (context().isDocumentOpen()) {
const AsyncResult result = asyncResult();
const CodeCompletedMessage message(result.completions,
result.correction,
context().jobRequest.ticketNumber);
context().client->codeCompleted(message);
}
}
} // namespace ClangBackEnd
/****************************************************************************
**
** 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 "clangasyncjob.h"
#include "clangtranslationunit.h"
#include <clangbackendipc/codecompletion.h>
namespace ClangBackEnd {
struct CompleteCodeJobResult
{
CodeCompletions completions;
CompletionCorrection correction = CompletionCorrection::NoCorrection;
};
class CompleteCodeJob : public AsyncJob<CompleteCodeJobResult>
{
public:
using AsyncResult = CompleteCodeJobResult;
bool prepareAsyncRun() override;
void finalizeAsyncRun() override;
private:
TranslationUnit m_pinnedTranslationUnit;
};
} // namespace ClangBackEnd
/****************************************************************************
**
** 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 "clangcreateinitialdocumentpreamblejob.h"
#include <clangbackendipc/clangbackendipcdebugutils.h>
#include <utils/qtcassert.h>
namespace ClangBackEnd {
static void runAsyncHelper(const TranslationUnitCore &translationUnitCore,
const TranslationUnitUpdateInput &translationUnitUpdateInput)
{
TIME_SCOPE_DURATION("CreateInitialDocumentPreambleJobRunner");
try {
translationUnitCore.reparse(translationUnitUpdateInput);
} catch (const std::exception &exception) {
qWarning() << "Error in CreateInitialDocumentPreambleJobRunner:" << exception.what();
}
}
bool CreateInitialDocumentPreambleJob::prepareAsyncRun()
{
const JobRequest jobRequest = context().jobRequest;
QTC_ASSERT(jobRequest.type == JobRequest::Type::CreateInitialDocumentPreamble, return false);
try {
m_pinnedTranslationUnit = context().translationUnitForJobRequest();
m_pinnedFileContainer = m_pinnedTranslationUnit.fileContainer();
const TranslationUnitCore translationUnitCore = m_pinnedTranslationUnit.translationUnitCore();
const TranslationUnitUpdateInput updateInput = m_pinnedTranslationUnit.createUpdateInput();