summaryrefslogtreecommitdiff
path: root/src/tools/clangbackend/ipcsource/clangjobqueue.cpp
blob: 383c2ecaa45bab9d7f9a1dcdfbe5d67be6f28fba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/****************************************************************************
**
** 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 "clangjobqueue.h"
#include "clangdocument.h"
#include "clangdocuments.h"
#include "clangtranslationunits.h"
#include "projects.h"
#include "unsavedfiles.h"

#include <utils/algorithm.h>

namespace ClangBackEnd {

JobQueue::JobQueue(Documents &documents, ProjectParts &projectParts)
    : m_documents(documents)
    , m_projectParts(projectParts)
{
}

bool JobQueue::add(const JobRequest &job)
{
    if (m_queue.contains(job)) {
        qCDebug(jobsLog) << "Not adding duplicate request" << job;
        return false;
    }

    if (isJobRunningForJobRequest(job)) {
        qCDebug(jobsLog) << "Not adding duplicate request for already running job" << job;
        return false;
    }

    if (!m_documents.hasDocument(job.filePath, job.projectPartId)) {
        qCDebug(jobsLog) << "Not adding / cancelling due to already closed document:" << job;
        cancelJobRequest(job);
        return false;
    }

    const Document document = m_documents.document(job.filePath, job.projectPartId);
    if (!document.isIntact()) {
        qCDebug(jobsLog) << "Not adding / cancelling due not intact document:" << job;
        cancelJobRequest(job);
        return false;
    }

    qCDebug(jobsLog) << "Adding" << job;
    m_queue.append(job);

    return true;
}

int JobQueue::size() const
{
    return m_queue.size();
}

JobRequests JobQueue::processQueue()
{
    removeExpiredRequests();
    prioritizeRequests();
    const JobRequests jobsToRun = takeJobRequestsToRunNow();

    return jobsToRun;
}

void JobQueue::removeExpiredRequests()
{
    JobRequests cleanedRequests;

    foreach (const JobRequest &jobRequest, m_queue) {
        try {
            if (!isJobRequestExpired(jobRequest))
                cleanedRequests.append(jobRequest);
        } catch (const std::exception &exception) {
            qWarning() << "Error in Jobs::removeOutDatedRequests for"
                       << jobRequest << ":" << exception.what();
        }
    }

    m_queue = cleanedRequests;
}

bool JobQueue::isJobRequestExpired(const JobRequest &jobRequest)
{
    const JobRequest::ExpirationReasons expirationReasons = jobRequest.expirationReasons;
    const UnsavedFiles unsavedFiles = m_documents.unsavedFiles();
    using ExpirationReason = JobRequest::ExpirationReason;

    if (expirationReasons.testFlag(ExpirationReason::UnsavedFilesChanged)) {
        if (jobRequest.unsavedFilesChangeTimePoint != unsavedFiles.lastChangeTimePoint()) {
            qCDebug(jobsLog) << "Removing due to outdated unsaved files:" << jobRequest;
            return true;
        }
    }

    bool projectCheckedAndItExists = false;

    if (expirationReasons.testFlag(ExpirationReason::DocumentClosed)) {
        if (!m_documents.hasDocument(jobRequest.filePath, jobRequest.projectPartId)) {
            qCDebug(jobsLog) << "Removing due to already closed document:" << jobRequest;
            return true;
        }

        if (!m_projectParts.hasProjectPart(jobRequest.projectPartId)) {
            qCDebug(jobsLog) << "Removing due to already closed project:" << jobRequest;
            return true;
        }
        projectCheckedAndItExists = true;

        const Document document
                = m_documents.document(jobRequest.filePath, jobRequest.projectPartId);
        if (!document.isIntact()) {
            qCDebug(jobsLog) << "Removing/Cancelling due to not intact document:" << jobRequest;
            cancelJobRequest(jobRequest);
            return true;
        }

        if (expirationReasons.testFlag(ExpirationReason::DocumentRevisionChanged)) {
            if (document.documentRevision() > jobRequest.documentRevision) {
                qCDebug(jobsLog) << "Removing due to changed document revision:" << jobRequest;
                return true;
            }
        }
    }

    if (expirationReasons.testFlag(ExpirationReason::ProjectChanged)) {
        if (!projectCheckedAndItExists && !m_projectParts.hasProjectPart(jobRequest.projectPartId)) {
            qCDebug(jobsLog) << "Removing due to already closed project:" << jobRequest;
            return true;
        }

        const ProjectPart &project = m_projectParts.project(jobRequest.projectPartId);
        if (project.lastChangeTimePoint() != jobRequest.projectChangeTimePoint) {
            qCDebug(jobsLog) << "Removing due to outdated project:" << jobRequest;
            return true;
        }
    }

    return false;
}

static int priority(const Document &document)
{
    int thePriority = 0;

    if (document.isUsedByCurrentEditor())
        thePriority += 1000;

    if (document.isVisibleInEditor())
        thePriority += 100;

    return thePriority;
}

void JobQueue::prioritizeRequests()
{
    const auto lessThan = [this] (const JobRequest &r1, const JobRequest &r2) {
        // TODO: Getting the TU is O(n) currently, so this might become expensive for large n.
        const Document &t1 = m_documents.document(r1.filePath, r1.projectPartId);
        const Document &t2 = m_documents.document(r2.filePath, r2.projectPartId);

        return priority(t1) > priority(t2);
    };

    std::stable_sort(m_queue.begin(), m_queue.end(), lessThan);
}

void JobQueue::cancelJobRequest(const JobRequest &jobRequest)
{
    if (m_cancelJobRequest)
        m_cancelJobRequest(jobRequest);
}

static bool passesPreconditions(const JobRequest &request, const Document &document)
{
    using Condition = JobRequest::Condition;
    const JobRequest::Conditions conditions = request.conditions;

    if (conditions.testFlag(Condition::DocumentSuspended) && !document.isSuspended()) {
        qCDebug(jobsLog) << "Not choosing due to unsuspended document:" << request;
        return false;
    }

    if (conditions.testFlag(Condition::DocumentUnsuspended) && document.isSuspended()) {
        qCDebug(jobsLog) << "Not choosing due to suspended document:" << request;
        return false;
    }

    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;
    using TranslationUnitIds = QSet<Utf8String>;
    TranslationUnitIds translationUnitsScheduledForThisRun;

    QMutableVectorIterator<JobRequest> i(m_queue);
    while (i.hasNext()) {
        const JobRequest &request = i.next();

        try {
            const Document &document = m_documents.document(request.filePath,
                                                            request.projectPartId);

            if (!passesPreconditions(request, document))
                continue;

            const Utf8String id = document.translationUnit(request.preferredTranslationUnit).id();
            if (translationUnitsScheduledForThisRun.contains(id))
                continue;

            if (isJobRunningForTranslationUnit(id))
                continue;

            translationUnitsScheduledForThisRun.insert(id);
            jobsToRun += request;
            i.remove();
        } catch (const std::exception &exception) {
            qWarning() << "Error in Jobs::takeJobRequestsToRunNow for"
                       << request << ":" << exception.what();
        }
    }

    return jobsToRun;
}

bool JobQueue::isJobRunningForTranslationUnit(const Utf8String &translationUnitId)
{
    if (m_isJobRunningForTranslationUnitHandler)
        return m_isJobRunningForTranslationUnitHandler(translationUnitId);

    return false;
}

bool JobQueue::isJobRunningForJobRequest(const JobRequest &jobRequest)
{
    if (m_isJobRunningForJobRequestHandler)
        return m_isJobRunningForJobRequestHandler(jobRequest);

    return false;
}

void JobQueue::setIsJobRunningForTranslationUnitHandler(
        const IsJobRunningForTranslationUnitHandler &isJobRunningHandler)
{
    m_isJobRunningForTranslationUnitHandler = isJobRunningHandler;
}

void JobQueue::setIsJobRunningForJobRequestHandler(
        const JobQueue::IsJobRunningForJobRequestHandler &isJobRunningHandler)
{
    m_isJobRunningForJobRequestHandler = isJobRunningHandler;
}

void JobQueue::setCancelJobRequest(const JobQueue::CancelJobRequest &cancelJobRequest)
{
    m_cancelJobRequest = cancelJobRequest;
}

JobRequests &JobQueue::queue()
{
    return m_queue;
}

} // namespace ClangBackEnd