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 \
......@@ -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();
setRunner([translationUnitCore, updateInput]() {
return runAsyncHelper(translationUnitCore, updateInput);
});
} catch (const std::exception &exception) {
qWarning() << "Error in CreateInitialDocumentPreambleJob::prepareAsyncRun:"
<< exception.what();
return false;
}
return true;
}
void CreateInitialDocumentPreambleJob::finalizeAsyncRun()
{
}
} // namespace ClangBackEnd
......@@ -25,22 +25,20 @@
#pragma once
#include <gmock/gmock.h>
#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>
#include "gtest-qt-printing.h"
#include "clangasyncjob.h"
#include "clangtranslationunit.h"
class SendDocumentAnnotationsCallback
namespace ClangBackEnd {
class CreateInitialDocumentPreambleJob : public AsyncJob<void>
{
public:
virtual ~SendDocumentAnnotationsCallback() = default;
bool prepareAsyncRun() override;
void finalizeAsyncRun() override;
virtual void sendDocumentAnnotations() = 0;
private:
TranslationUnit m_pinnedTranslationUnit;
FileContainer m_pinnedFileContainer;
};
class MockSendDocumentAnnotationsCallback : public SendDocumentAnnotationsCallback
{
public:
MOCK_METHOD0(sendDocumentAnnotations,
void());
};
} // 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 "clangiasyncjob.h"
#include "clangcompletecodejob.h"
#include "clangcreateinitialdocumentpreamblejob.h"
#include "clangrequestdocumentannotationsjob.h"
#include "clangupdatedocumentannotationsjob.h"
Q_LOGGING_CATEGORY(jobsLog, "qtc.clangbackend.jobs");
namespace ClangBackEnd {
IAsyncJob *IAsyncJob::create(JobRequest::Type type)
{
switch (type) {
case JobRequest::Type::UpdateDocumentAnnotations:
return new UpdateDocumentAnnotationsJob();
case JobRequest::Type::CreateInitialDocumentPreamble:
return new CreateInitialDocumentPreambleJob();
case JobRequest::Type::CompleteCode:
return new CompleteCodeJob();
case JobRequest::Type::RequestDocumentAnnotations:
return new RequestDocumentAnnotationsJob();
}
return nullptr;
}
IAsyncJob::IAsyncJob()
: m_context(JobContext())
{
}
IAsyncJob::~IAsyncJob()
{
}
JobContext IAsyncJob::context() const
{
return m_context;
}
void IAsyncJob::setContext(const JobContext &context)
{
m_context = context;
}
IAsyncJob::FinishedHandler IAsyncJob::finishedHandler() const
{
return m_finishedHandler;
}
void IAsyncJob::setFinishedHandler(const IAsyncJob::FinishedHandler &finishedHandler)
{
m_finishedHandler = finishedHandler;
}
bool IAsyncJob::isFinished() const
{
return m_isFinished;
}
void IAsyncJob::setIsFinished(bool isFinished)
{
m_isFinished = isFinished;
}
} // 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 "clangjobcontext.h"
#include <QFuture>
#include <QLoggingCategory>
#include <functional>
Q_DECLARE_LOGGING_CATEGORY(jobsLog);
namespace ClangBackEnd {
class IAsyncJob
{
public:
static IAsyncJob *create(JobRequest::Type type);
public:
IAsyncJob();
virtual ~IAsyncJob();
JobContext context() const;
void setContext(const JobContext &context);
using FinishedHandler = std::function<void(IAsyncJob *job)>;
FinishedHandler finishedHandler() const;
void setFinishedHandler(const FinishedHandler &finishedHandler);
virtual bool prepareAsyncRun() = 0;
virtual QFuture<void> runAsync() = 0;
virtual void finalizeAsyncRun() = 0;
public: // for tests
bool isFinished() const;
void setIsFinished(bool isFinished);
private:
bool m_isFinished = false;
FinishedHandler m_finishedHandler;
JobContext m_context;
};
} // 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 "clangiasyncjob.h"
#include "translationunits.h"