Clang: Fix canceling of clang query

Every AST unit is created and queried asynchronously, like before, but
we don't wait anymore but use a timer to process new sources. So the server
will not be blocked and can process other messages like cancel.

Change-Id: If0e69466c78f628190f59fd32a03cab1c3a4d0a3
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Marco Bubke
2017-06-26 14:00:32 +02:00
committed by Tim Jenssen
parent a30a18177b
commit f5d68398d5
10 changed files with 645 additions and 107 deletions

View File

@@ -0,0 +1,160 @@
/****************************************************************************
**
** Copyright (C) 2017 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 "clangquerygatherer.h"
#include "clangquery.h"
namespace ClangBackEnd {
ClangQueryGatherer::ClangQueryGatherer(std::vector<V2::FileContainer> &&sources,
std::vector<V2::FileContainer> &&unsaved,
Utils::SmallString &&query)
: m_sources(std::move(sources)),
m_unsaved(std::move(unsaved)),
m_query(std::move(query))
{
}
SourceRangesAndDiagnosticsForQueryMessage
ClangQueryGatherer::createSourceRangesAndDiagnosticsForSource(
V2::FileContainer &&source,
const std::vector<V2::FileContainer> &unsaved,
Utils::SmallString &&query)
{
ClangQuery clangQuery(std::move(query));
clangQuery.addFile(source.filePath().directory(),
source.filePath().name(),
source.takeUnsavedFileContent(),
source.takeCommandLineArguments());
clangQuery.addUnsavedFiles(unsaved);
clangQuery.findLocations();
return {clangQuery.takeSourceRanges(), clangQuery.takeDiagnosticContainers()};
}
bool ClangQueryGatherer::canCreateSourceRangesAndDiagnostics() const
{
return !m_sources.empty();
}
SourceRangesAndDiagnosticsForQueryMessage ClangQueryGatherer::createNextSourceRangesAndDiagnostics()
{
auto message = createSourceRangesAndDiagnosticsForSource(std::move(m_sources.back()),
m_unsaved,
m_query.clone());
m_sources.pop_back();
return message;
}
ClangQueryGatherer::Future ClangQueryGatherer::startCreateNextSourceRangesAndDiagnosticsMessage()
{
Future future = std::async(std::launch::async,
createSourceRangesAndDiagnosticsForSource,
std::move(m_sources.back()),
m_unsaved,
m_query.clone());
m_sources.pop_back();
return future;
}
void ClangQueryGatherer::startCreateNextSourceRangesAndDiagnosticsMessages()
{
std::vector<ClangQueryGatherer::Future> futures;
while (!m_sources.empty() && m_sourceFutures.size() < m_processingSlotCount)
m_sourceFutures.push_back(startCreateNextSourceRangesAndDiagnosticsMessage());
}
void ClangQueryGatherer::waitForFinished()
{
for (Future &future : m_sourceFutures)
future.wait();
}
bool ClangQueryGatherer::isFinished() const
{
return m_sources.empty() && m_sourceFutures.empty();
}
const std::vector<V2::FileContainer> &ClangQueryGatherer::sources() const
{
return m_sources;
}
const std::vector<ClangQueryGatherer::Future> &ClangQueryGatherer::sourceFutures() const
{
return m_sourceFutures;
}
std::vector<SourceRangesAndDiagnosticsForQueryMessage> ClangQueryGatherer::allCurrentProcessedMessages()
{
std::vector<SourceRangesAndDiagnosticsForQueryMessage> messages;
for (Future &future : m_sourceFutures)
messages.push_back(future.get());
return messages;
}
std::vector<SourceRangesAndDiagnosticsForQueryMessage> ClangQueryGatherer::finishedMessages()
{
std::vector<SourceRangesAndDiagnosticsForQueryMessage> messages;
for (auto &&future : finishedFutures())
messages.push_back(future.get());
return messages;
}
void ClangQueryGatherer::setProcessingSlotCount(uint count)
{
m_processingSlotCount = count;
}
std::vector<ClangQueryGatherer::Future> ClangQueryGatherer::finishedFutures()
{
std::vector<Future> finishedFutures;
finishedFutures.reserve(m_sourceFutures.size());
auto beginReady = std::partition(m_sourceFutures.begin(),
m_sourceFutures.end(),
[] (const Future &future) {
return future.wait_for(std::chrono::duration<int>::zero()) != std::future_status::ready;
});
std::move(beginReady, m_sourceFutures.end(), std::back_inserter(finishedFutures));
m_sourceFutures.erase(beginReady, m_sourceFutures.end());
return finishedFutures;
}
} // namespace ClangBackEnd

View File

@@ -0,0 +1,76 @@
/****************************************************************************
**
** Copyright (C) 2017 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 <sourcerangesanddiagnosticsforquerymessage.h>
#include <filecontainerv2.h>
#include <future>
namespace ClangBackEnd {
class ClangQueryGatherer
{
public:
using Future = std::future<SourceRangesAndDiagnosticsForQueryMessage>;
ClangQueryGatherer() = default;
ClangQueryGatherer(std::vector<V2::FileContainer> &&sources,
std::vector<V2::FileContainer> &&unsaved,
Utils::SmallString &&query);
static
SourceRangesAndDiagnosticsForQueryMessage createSourceRangesAndDiagnosticsForSource(
V2::FileContainer &&source,
const std::vector<V2::FileContainer> &unsaved,
Utils::SmallString &&query);
bool canCreateSourceRangesAndDiagnostics() const;
SourceRangesAndDiagnosticsForQueryMessage createNextSourceRangesAndDiagnostics();
Future startCreateNextSourceRangesAndDiagnosticsMessage();
void startCreateNextSourceRangesAndDiagnosticsMessages();
void waitForFinished();
bool isFinished() const;
const std::vector<V2::FileContainer> &sources() const;
const std::vector<Future> &sourceFutures() const;
std::vector<SourceRangesAndDiagnosticsForQueryMessage> allCurrentProcessedMessages();
std::vector<SourceRangesAndDiagnosticsForQueryMessage> finishedMessages();
void setProcessingSlotCount(uint count);
protected:
std::vector<Future> finishedFutures();
private:
std::vector<V2::FileContainer> m_sources;
std::vector<V2::FileContainer> m_unsaved;
Utils::SmallString m_query;
uint m_processingSlotCount = 1;
std::vector<Future> m_sourceFutures;
};
} // namespace ClangBackEnd

View File

@@ -2,6 +2,7 @@ INCLUDEPATH += $$PWD
HEADERS += \
$$PWD/clangrefactoringbackend_global.h \
$$PWD/clangquerygatherer.h
!isEmpty(LIBTOOLING_LIBS) {
SOURCES += \
@@ -31,3 +32,6 @@ HEADERS += \
$$PWD/sourcerangeextractor.h \
$$PWD/locationsourcefilecallbacks.h
}
SOURCES += \
$$PWD/clangquerygatherer.cpp

View File

@@ -36,16 +36,17 @@
#include <QCoreApplication>
#include <algorithm>
#include <chrono>
#include <future>
#include <atomic>
#include <functional>
namespace ClangBackEnd {
RefactoringServer::RefactoringServer()
{
pollEventLoop = [] () { QCoreApplication::processEvents(); };
m_pollTimer.setInterval(100);
QObject::connect(&m_pollTimer,
&QTimer::timeout,
std::bind(&RefactoringServer::pollSourceRangesAndDiagnosticsForQueryMessages, this));
}
void RefactoringServer::end()
@@ -72,110 +73,62 @@ void RefactoringServer::requestSourceLocationsForRenamingMessage(RequestSourceLo
void RefactoringServer::requestSourceRangesAndDiagnosticsForQueryMessage(
RequestSourceRangesAndDiagnosticsForQueryMessage &&message)
{
gatherSourceRangesAndDiagnosticsForQueryMessage(message.takeSources(),
message.takeUnsavedContent(),
message.takeQuery());
gatherSourceRangesAndDiagnosticsForQueryMessages(message.takeSources(),
message.takeUnsavedContent(),
message.takeQuery());
}
void RefactoringServer::cancel()
{
cancelWork = true;
m_gatherer.waitForFinished();
m_gatherer = ClangQueryGatherer();
m_pollTimer.stop();
}
bool RefactoringServer::isCancelingJobs() const
{
return cancelWork;
return m_gatherer.isFinished();
}
void RefactoringServer::supersedePollEventLoop(std::function<void ()> &&pollEventLoop)
void RefactoringServer::pollSourceRangesAndDiagnosticsForQueryMessages()
{
this->pollEventLoop = std::move(pollEventLoop);
for (auto &&message : m_gatherer.finishedMessages())
client()->sourceRangesAndDiagnosticsForQueryMessage(std::move(message));
if (!m_gatherer.isFinished())
m_gatherer.startCreateNextSourceRangesAndDiagnosticsMessages();
else
m_pollTimer.stop();
}
namespace {
SourceRangesAndDiagnosticsForQueryMessage createSourceRangesAndDiagnosticsForQueryMessage(
V2::FileContainer &&source,
std::vector<V2::FileContainer> &&unsaved,
Utils::SmallString &&query,
const std::atomic_bool &cancelWork) {
ClangQuery clangQuery(std::move(query));
if (!cancelWork) {
clangQuery.addFile(source.filePath().directory(),
source.filePath().name(),
source.takeUnsavedFileContent(),
source.takeCommandLineArguments());
clangQuery.addUnsavedFiles(std::move(unsaved));
clangQuery.findLocations();
void RefactoringServer::waitThatSourceRangesAndDiagnosticsForQueryMessagesAreFinished()
{
while (!m_gatherer.isFinished()) {
m_gatherer.waitForFinished();
pollSourceRangesAndDiagnosticsForQueryMessages();
}
return {clangQuery.takeSourceRanges(), clangQuery.takeDiagnosticContainers()};
}
bool RefactoringServer::pollTimerIsActive() const
{
return m_pollTimer.isActive();
}
void RefactoringServer::gatherSourceRangesAndDiagnosticsForQueryMessage(
void RefactoringServer::gatherSourceRangesAndDiagnosticsForQueryMessages(
std::vector<V2::FileContainer> &&sources,
std::vector<V2::FileContainer> &&unsaved,
Utils::SmallString &&query)
{
std::vector<Future> futures;
#ifdef _WIN32
std::size_t freeProcessors = 1;
uint freeProcessors = 1;
#else
std::size_t freeProcessors = std::thread::hardware_concurrency();
uint freeProcessors = std::thread::hardware_concurrency();
#endif
while (!sources.empty() || !futures.empty()) {
--freeProcessors;
m_gatherer = ClangQueryGatherer(std::move(sources), std::move(unsaved), std::move(query));
m_gatherer.setProcessingSlotCount(freeProcessors);
if (!sources.empty()) {
Future &&future = std::async(std::launch::async,
createSourceRangesAndDiagnosticsForQueryMessage,
std::move(sources.back()),
Utils::clone(unsaved),
query.clone(),
std::ref(cancelWork));
sources.pop_back();
futures.emplace_back(std::move(future));
}
if (freeProcessors == 0 || sources.empty())
freeProcessors += waitForNewSourceRangesAndDiagnosticsForQueryMessage(futures);
}
}
std::size_t RefactoringServer::waitForNewSourceRangesAndDiagnosticsForQueryMessage(std::vector<Future> &futures)
{
while (true) {
pollEventLoop();
std::vector<Future> readyFutures;
readyFutures.reserve(futures.size());
auto beginReady = std::partition(futures.begin(),
futures.end(),
[] (const Future &future) {
return future.wait_for(std::chrono::duration<int>::zero()) != std::future_status::ready;
});
std::move(beginReady, futures.end(), std::back_inserter(readyFutures));
futures.erase(beginReady, futures.end());
for (Future &readyFuture : readyFutures)
client()->sourceRangesAndDiagnosticsForQueryMessage(readyFuture.get());
if (readyFutures.empty())
std::this_thread::sleep_for(std::chrono::milliseconds(20));
else
return readyFutures.size();
}
m_pollTimer.start();
}
} // namespace ClangBackEnd

View File

@@ -25,9 +25,12 @@
#pragma once
#include "clangquerygatherer.h"
#include <refactoringserverinterface.h>
#include <future>
#include <QTimer>
#include <vector>
namespace ClangBackEnd {
@@ -51,18 +54,19 @@ public:
bool isCancelingJobs() const;
void supersedePollEventLoop(std::function<void()> &&pollEventLoop);
void pollSourceRangesAndDiagnosticsForQueryMessages();
void waitThatSourceRangesAndDiagnosticsForQueryMessagesAreFinished();
bool pollTimerIsActive() const;
private:
void gatherSourceRangesAndDiagnosticsForQueryMessage(std::vector<V2::FileContainer> &&sources,
std::vector<V2::FileContainer> &&unsaved,
Utils::SmallString &&query);
std::size_t waitForNewSourceRangesAndDiagnosticsForQueryMessage(std::vector<Future> &futures);
void gatherSourceRangesAndDiagnosticsForQueryMessages(std::vector<V2::FileContainer> &&sources,
std::vector<V2::FileContainer> &&unsaved,
Utils::SmallString &&query);
private:
std::function<void()> pollEventLoop;
std::atomic_bool cancelWork{false};
ClangQueryGatherer m_gatherer;
QTimer m_pollTimer;
};
} // namespace ClangBackEnd