forked from qt-creator/qt-creator
ClangRefactoring: Fix progress counter
We use threads now and the code had to be adapted. Task-number: QTCREATORBUG-21950 Change-Id: Ie96c5bea1fa045588d0c5a8b18bfd1ccb5d9fdd9 Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
This commit is contained in:
@@ -26,6 +26,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
namespace ClangBackEnd {
|
namespace ClangBackEnd {
|
||||||
|
|
||||||
@@ -33,6 +34,7 @@ class ProgressCounter
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using SetProgressCallback = std::function<void(int, int)>;
|
using SetProgressCallback = std::function<void(int, int)>;
|
||||||
|
using Lock = std::lock_guard<std::mutex>;
|
||||||
|
|
||||||
ProgressCounter(SetProgressCallback &&progressCallback)
|
ProgressCounter(SetProgressCallback &&progressCallback)
|
||||||
: m_progressCallback(std::move(progressCallback))
|
: m_progressCallback(std::move(progressCallback))
|
||||||
@@ -40,6 +42,8 @@ public:
|
|||||||
|
|
||||||
void addTotal(int total)
|
void addTotal(int total)
|
||||||
{
|
{
|
||||||
|
Lock lock(m_mutex);
|
||||||
|
|
||||||
if (total) {
|
if (total) {
|
||||||
m_total += total;
|
m_total += total;
|
||||||
|
|
||||||
@@ -49,6 +53,8 @@ public:
|
|||||||
|
|
||||||
void removeTotal(int total)
|
void removeTotal(int total)
|
||||||
{
|
{
|
||||||
|
Lock lock(m_mutex);
|
||||||
|
|
||||||
if (total) {
|
if (total) {
|
||||||
m_total -= total;
|
m_total -= total;
|
||||||
|
|
||||||
@@ -58,6 +64,8 @@ public:
|
|||||||
|
|
||||||
void addProgress(int progress)
|
void addProgress(int progress)
|
||||||
{
|
{
|
||||||
|
Lock lock(m_mutex);
|
||||||
|
|
||||||
if (progress) {
|
if (progress) {
|
||||||
m_progress += progress;
|
m_progress += progress;
|
||||||
|
|
||||||
@@ -65,6 +73,21 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int total() const
|
||||||
|
{
|
||||||
|
Lock lock(m_mutex);
|
||||||
|
|
||||||
|
return m_total;
|
||||||
|
}
|
||||||
|
|
||||||
|
int progress() const
|
||||||
|
{
|
||||||
|
Lock lock(m_mutex);
|
||||||
|
|
||||||
|
return m_total;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
void sendProgress()
|
void sendProgress()
|
||||||
{
|
{
|
||||||
m_progressCallback(m_progress, m_total);
|
m_progressCallback(m_progress, m_total);
|
||||||
@@ -75,17 +98,8 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int total() const
|
|
||||||
{
|
|
||||||
return m_total;
|
|
||||||
}
|
|
||||||
|
|
||||||
int progress() const
|
|
||||||
{
|
|
||||||
return m_total;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
mutable std::mutex m_mutex;
|
||||||
std::function<void(int, int)> m_progressCallback;
|
std::function<void(int, int)> m_progressCallback;
|
||||||
int m_progress = 0;
|
int m_progress = 0;
|
||||||
int m_total = 0;
|
int m_total = 0;
|
||||||
|
@@ -51,6 +51,48 @@ using ClangBackEnd::RefactoringDatabaseInitializer;
|
|||||||
using ClangBackEnd::ConnectionServer;
|
using ClangBackEnd::ConnectionServer;
|
||||||
using ClangBackEnd::SymbolIndexing;
|
using ClangBackEnd::SymbolIndexing;
|
||||||
|
|
||||||
|
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
|
||||||
|
template<typename CallableType>
|
||||||
|
class CallableEvent : public QEvent
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using Callable = std::decay_t<CallableType>;
|
||||||
|
CallableEvent(Callable &&callable)
|
||||||
|
: QEvent(QEvent::None)
|
||||||
|
, callable(std::move(callable))
|
||||||
|
{}
|
||||||
|
CallableEvent(const Callable &callable)
|
||||||
|
: QEvent(QEvent::None)
|
||||||
|
, callable(callable)
|
||||||
|
{}
|
||||||
|
|
||||||
|
~CallableEvent() { callable(); }
|
||||||
|
|
||||||
|
public:
|
||||||
|
Callable callable;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Callable>
|
||||||
|
void executeInLoop(Callable &&callable, QObject *object = QCoreApplication::instance())
|
||||||
|
{
|
||||||
|
if (QThread *thread = qobject_cast<QThread *>(object))
|
||||||
|
object = QAbstractEventDispatcher::instance(thread);
|
||||||
|
|
||||||
|
QCoreApplication::postEvent(object,
|
||||||
|
new CallableEvent<Callable>(std::forward<Callable>(callable)),
|
||||||
|
Qt::HighEventPriority);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
template<typename Callable>
|
||||||
|
void executeInLoop(Callable &&callable, QObject *object = QCoreApplication::instance())
|
||||||
|
{
|
||||||
|
if (QThread *thread = qobject_cast<QThread *>(object))
|
||||||
|
object = QAbstractEventDispatcher::instance(thread);
|
||||||
|
|
||||||
|
QMetaObject::invokeMethod(object, std::forward<Callable>(callable));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
QStringList processArguments(QCoreApplication &application)
|
QStringList processArguments(QCoreApplication &application)
|
||||||
{
|
{
|
||||||
QCommandLineParser parser;
|
QCommandLineParser parser;
|
||||||
@@ -95,7 +137,14 @@ struct Data // because we have a cycle dependency
|
|||||||
FilePathCaching filePathCache{database};
|
FilePathCaching filePathCache{database};
|
||||||
GeneratedFiles generatedFiles;
|
GeneratedFiles generatedFiles;
|
||||||
RefactoringServer clangCodeModelServer{symbolIndexing, filePathCache, generatedFiles};
|
RefactoringServer clangCodeModelServer{symbolIndexing, filePathCache, generatedFiles};
|
||||||
SymbolIndexing symbolIndexing{database, filePathCache, generatedFiles, [&] (int progress, int total) { clangCodeModelServer.setProgress(progress, total); }};
|
SymbolIndexing symbolIndexing{database,
|
||||||
|
filePathCache,
|
||||||
|
generatedFiles,
|
||||||
|
[&](int progress, int total) {
|
||||||
|
executeInLoop([&] {
|
||||||
|
clangCodeModelServer.setProgress(progress, total);
|
||||||
|
});
|
||||||
|
}};
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
|
@@ -417,7 +417,7 @@ TYPED_TEST(CommandLineBuilder, C18)
|
|||||||
|
|
||||||
Builder<TypeParam> builder{this->emptyProjectInfo, {}, InputFileType::Header, "/source/file.c"};
|
Builder<TypeParam> builder{this->emptyProjectInfo, {}, InputFileType::Header, "/source/file.c"};
|
||||||
|
|
||||||
ASSERT_THAT(builder.commandLine, Contains("-std=c18"));
|
ASSERT_THAT(builder.commandLine, Contains("-std=c17"));
|
||||||
}
|
}
|
||||||
|
|
||||||
TYPED_TEST(CommandLineBuilder, GnuC89)
|
TYPED_TEST(CommandLineBuilder, GnuC89)
|
||||||
@@ -461,7 +461,7 @@ TYPED_TEST(CommandLineBuilder, GnuC18)
|
|||||||
|
|
||||||
Builder<TypeParam> builder{this->emptyProjectInfo, {}, InputFileType::Header, "/source/file.c"};
|
Builder<TypeParam> builder{this->emptyProjectInfo, {}, InputFileType::Header, "/source/file.c"};
|
||||||
|
|
||||||
ASSERT_THAT(builder.commandLine, Contains("-std=gnu18"));
|
ASSERT_THAT(builder.commandLine, Contains("-std=gnu17"));
|
||||||
}
|
}
|
||||||
|
|
||||||
TYPED_TEST(CommandLineBuilder, IncludesOrder)
|
TYPED_TEST(CommandLineBuilder, IncludesOrder)
|
||||||
|
Reference in New Issue
Block a user