From c2cfc62f08f8c04b4795cc13cfd460153749c653 Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Fri, 28 Jul 2017 15:13:23 +0200 Subject: [PATCH] Clang: Use document visibility as jobrequest conditions This is in preparation for a follow-up change that will add jobs that also operate on invisible documents. Change-Id: I2a0fc3b95cc5ab2e267429134a534df64c901376 Reviewed-by: Ivan Donchevskii Reviewed-by: Marco Bubke --- .../clangbackend/ipcsource/clangjobqueue.cpp | 48 +++++++++++++------ .../ipcsource/clangjobrequest.cpp | 5 +- .../clangbackend/ipcsource/clangjobrequest.h | 6 ++- tests/unit/unittest/clangjobqueue-test.cpp | 23 +-------- 4 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/tools/clangbackend/ipcsource/clangjobqueue.cpp b/src/tools/clangbackend/ipcsource/clangjobqueue.cpp index 341c9a89def..7af76ce32d4 100644 --- a/src/tools/clangbackend/ipcsource/clangjobqueue.cpp +++ b/src/tools/clangbackend/ipcsource/clangjobqueue.cpp @@ -174,6 +174,38 @@ void JobQueue::prioritizeRequests() std::stable_sort(m_queue.begin(), m_queue.end(), lessThan); } +static bool passesPreconditions(const JobRequest &request, const Document &document) +{ + using Condition = JobRequest::Condition; + const JobRequest::Conditions conditions = request.conditions; + + if (conditions.testFlag(Condition::DocumentVisible) && !document.isVisibleInEditor()) { + qCDebug(jobsLog) << "Not choosing due to invisble document:" << request; + return false; + } + + if (conditions.testFlag(Condition::DocumentNotVisible) && document.isVisibleInEditor()) { + qCDebug(jobsLog) << "Not choosing due to visble document:" << request; + return false; + } + + if (conditions.testFlag(Condition::CurrentDocumentRevision)) { + if (document.isDirty()) { + // TODO: If the document is dirty due to a project update, + // references are processes later than ideal. + qCDebug(jobsLog) << "Not choosing due to dirty document:" << request; + return false; + } + + if (request.documentRevision != document.documentRevision()) { + qCDebug(jobsLog) << "Not choosing due to revision mismatch:" << request; + return false; + } + } + + return true; +} + JobRequests JobQueue::takeJobRequestsToRunNow() { JobRequests jobsToRun; @@ -188,7 +220,7 @@ JobRequests JobQueue::takeJobRequestsToRunNow() const Document &document = m_documents.document(request.filePath, request.projectPartId); - if (!document.isUsedByCurrentEditor() && !document.isVisibleInEditor()) + if (!passesPreconditions(request, document)) continue; const Utf8String id = document.translationUnit(request.preferredTranslationUnit).id(); @@ -198,20 +230,6 @@ JobRequests JobQueue::takeJobRequestsToRunNow() if (isJobRunningForTranslationUnit(id)) continue; - if (request.conditions.testFlag(JobRequest::Condition::CurrentDocumentRevision)) { - if (document.isDirty()) { - // TODO: If the document is dirty due to a project update, - // references are processes later than ideal. - qCDebug(jobsLog) << "Not choosing due to dirty document:" << request; - continue; - } - - if (request.documentRevision != document.documentRevision()) { - qCDebug(jobsLog) << "Not choosing due to revision mismatch:" << request; - continue; - } - } - translationUnitsScheduledForThisRun.insert(id); jobsToRun += request; i.remove(); diff --git a/src/tools/clangbackend/ipcsource/clangjobrequest.cpp b/src/tools/clangbackend/ipcsource/clangjobrequest.cpp index 1e5032373a0..c9162b21258 100644 --- a/src/tools/clangbackend/ipcsource/clangjobrequest.cpp +++ b/src/tools/clangbackend/ipcsource/clangjobrequest.cpp @@ -121,10 +121,11 @@ JobRequest::ExpirationReasons JobRequest::expirationReasonsForType(Type type) JobRequest::Conditions JobRequest::conditionsForType(JobRequest::Type type) { + Conditions conditions = Condition::DocumentVisible; if (type == Type::RequestReferences) - return Conditions(Condition::CurrentDocumentRevision); + conditions |= Condition::CurrentDocumentRevision; - return Conditions(Condition::NoCondition); + return conditions; } } // namespace ClangBackEnd diff --git a/src/tools/clangbackend/ipcsource/clangjobrequest.h b/src/tools/clangbackend/ipcsource/clangjobrequest.h index 0b47d681903..b484dee7ce4 100644 --- a/src/tools/clangbackend/ipcsource/clangjobrequest.h +++ b/src/tools/clangbackend/ipcsource/clangjobrequest.h @@ -56,8 +56,10 @@ public: }; enum class Condition { - NoCondition, - CurrentDocumentRevision, + NoCondition = 1 << 0, + DocumentVisible = 1 << 1, + DocumentNotVisible = 1 << 2, + CurrentDocumentRevision = 1 << 3, }; Q_DECLARE_FLAGS(Conditions, Condition) diff --git a/tests/unit/unittest/clangjobqueue-test.cpp b/tests/unit/unittest/clangjobqueue-test.cpp index 1eb05fc0316..1b295acf764 100644 --- a/tests/unit/unittest/clangjobqueue-test.cpp +++ b/tests/unit/unittest/clangjobqueue-test.cpp @@ -65,9 +65,6 @@ protected: JobRequest::Type type, PreferredTranslationUnit preferredTranslationUnit = PreferredTranslationUnit::RecentlyParsed) const; - JobRequest createJobRequestWithConditions(const Utf8String &filePath, - JobRequest::Type type, - JobRequest::Conditions conditions) const; void updateDocumentRevision(); void updateUnsavedFiles(); @@ -415,9 +412,7 @@ TEST_F(JobQueue, RequestCompleteCodeOutdatableByDocumentRevisionChange) TEST_F(JobQueue, RequestReferencesRunsForCurrentDocumentRevision) { - jobQueue.add( createJobRequestWithConditions(filePath1, - JobRequest::Type::RequestReferences, - JobRequest::Condition::CurrentDocumentRevision)); + jobQueue.add(createJobRequest(filePath1, JobRequest::Type::RequestReferences)); const JobRequests jobsToStart = jobQueue.processQueue(); @@ -426,9 +421,7 @@ TEST_F(JobQueue, RequestReferencesRunsForCurrentDocumentRevision) TEST_F(JobQueue, RequestReferencesOutdatableByDocumentClose) { - jobQueue.add(createJobRequestWithConditions(filePath1, - JobRequest::Type::RequestReferences, - JobRequest::Condition::CurrentDocumentRevision)); + jobQueue.add(createJobRequest(filePath1, JobRequest::Type::RequestReferences)); removeDocument(); const JobRequests jobsToStart = jobQueue.processQueue(); @@ -488,18 +481,6 @@ JobRequest JobQueue::createJobRequest( return jobRequest; } -JobRequest JobQueue::createJobRequestWithConditions(const Utf8String &filePath, - JobRequest::Type type, - JobRequest::Conditions conditions) const -{ - JobRequest jobRequest = createJobRequest(filePath, - type, - PreferredTranslationUnit::RecentlyParsed); - jobRequest.conditions = conditions; - - return jobRequest; -} - void JobQueue::updateDocumentRevision() { documents.update({FileContainer(filePath1, projectPartId, Utf8String(), true, 1)});